即使应用程序关闭(已杀死)也运行服务

时间:2013-09-16 11:41:16

标签: android service background

即使应用程序已关闭(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);
        }
}

我怎么能这样做?

4 个答案:

答案 0 :(得分:10)

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

只要让它连续运行,请记住,通过设计,Android系统可以随时终止您的服务流程。您可以稍微影响一下,但无法阻止系统终止您的服务。

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

对于某些代码,请查看此处:

Android: Alarm Manager

答案 1 :(得分:5)

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

答案 2 :(得分:1)

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

来自活动,您可以通过

启动服务
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>

答案 3 :(得分:1)

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

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