【问题标题】:run service even if the application is closed (killed)即使应用程序关闭(被杀死)也运行服务
【发布时间】:2013-09-16 11:41:15
【问题描述】:

我希望即使应用程序关闭(kiiled)或者即使用户不启动应用程序,该服务也能运行。 我希望在安装应用程序后启动服务,从这一点开始,服务应该一直运行。

public class notifications extends Service {

        @Override
        public IBinder onBind(Intent intent) {
                // TODO Auto-generated method stub
                return null;
        }

        @Override
    public void onCreate() {
    }

    @Override
    public void onStart(Intent intent, int startId) {
                final Handler handler = new Handler();
                final Runnable runb = new Runnable()
                {
                    public void run()
                    {
                        Toast.makeText(getApplicationContext(), " Service Started", Toast.LENGTH_LONG).show();
                        handler.postDelayed(this, 10000);
                    }
                };
                handler.postDelayed(runb, 0);

    }
    @Override
    public void onDestroy() {

    }
}*/

public class notifications extends IntentService
{
        private Timer mBackGroundTimer;
        public notifications()
    {
        super("myservice");
        this.mBackGroundTimer=new Timer();
    }
        @Override
        protected void onHandleIntent(Intent intent)
    {
        // TODO Auto-generated method stub
        mBackGroundTimer.schedule(new TimerTask()
        {
            public void run()
            {
                try
                {
                        Notification("This is message from Dipak Keshariya (Android Application Developer)", "This is Android Notification Message");
                }
                catch (Exception e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        },1000, 2000);
    } // END onHandleIntent()
        private void mStopTimer()
    {
        //Call this whenever you need to stop the service
        mBackGroundTimer.cancel();
    }

        private void Notification(String notificationTitle, String notificationMessage) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message from Dipak Keshariya (Android Developer)!",
            System.currentTimeMillis());

            Intent notificationIntent = new Intent(this, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
            notification.setLatestEventInfo(getApplicationContext(), notificationTitle, notificationMessage, pendingIntent);
            notificationManager.notify(10001, notification);
        }
}

我该怎么做?

【问题讨论】:

标签: android service background


【解决方案1】:

查看您的代码,您似乎希望您的服务定期发出通知。

就让它持续运行而言,请记住,根据设计,Android 系统可能会随时终止您的服务进程。你可以稍微影响一下,但你不能阻止系统杀死你的服务。

因此,对于您的定期操作,最好将 AlarmManager 与重复警报一起使用。然后,您的服务基本上是一次性的,即执行一次操作然后退出。

对于一些代码,看这里例如:

Android: Alarm Manager

【讨论】:

  • 所以我把它放在我的 mainactivity 上并启动服务,即使我终止应用程序服务也不会停止运行?
  • 它将注册一个警报,该警报将定期启动您的服务。您的服务不需要一直运行(事实上,您的服务应该在您执行一次所需的操作后退出),因为您可以设置警报以在需要时触发单个服务运行。您的应用程序也无需运行。
  • 我想在 X 附加时发送通知,但 X 在网站上 (json)。所以我需要始终运行该服务,然后如果 X 附加我想发送通知。它需要一直运行。即使我没有启动应用程序。
  • 好吧,使用警报管理器方法,您的服务将不需要一直运行。您的服务在执行时唯一需要做的就是检查您的条件 X 一次,如果为真则发送您的通知。警报管理器将负责每 x 秒调用一次您的服务,这与让您的服务始终运行基本相同。好处是,您不必担心android系统会杀死您的服务进程,因为每次需要完成任务时它都会重新启动。即使您的应用没有运行,因为警报管理器不依赖于您的应用。
【解决方案2】:

您需要实现 Service 类的 OnStartCommand 方法,并在其中返回 Service.START_STICKY。那会成功的。如果您终止应用程序,该服务将继续在后台运行。但是,如果您重新启动手机,我认为您还需要在您的应用中实现其他东西,例如启动服务或类似的东西。

【讨论】:

  • 我认为如果不先启动应用程序就无法启动服务。但是使用 START_STICKY,在您启动服务后,它不会停止。
  • 但我的要求是我必须在登录后应用程序 start.ex 后启动服务。
【解决方案3】:

您的要求是在后台运行该服务。您在使用该服务的正确轨道上,因为这仅用于后台运行目的。

从activitiy你可以通过

启动服务
startService(new Intent(activityName.this, serviceName.class));

或者如果您的应用程序没有任何活动,那么您可以将服务设置为应用程序的默认启动器和主启动器,方法是:

<service android:name="name of the service" >
 <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
 </service>

【讨论】:

  • 是的,但如果我终止应用程序或重新启动智能手机,服务将停止工作,但我希望他始终运行..
  • 杀死应用程序和重启设备是两件事。您可以在手机再次启动时放置一个广播接收器,您可以启动您的服务。并使您的服务具有粘性,这样系统就不会杀死它。
【解决方案4】:

我认为这个问题的答案是这样的:https://developer.android.com/training/sync-adapters/creating-sync-adapter.html

Google I/O 2013 中引入的同步适配器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多