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

tor*_*aro 15 service android 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);
        }
}
Run Code Online (Sandbox Code Playgroud)

我怎么能这样做?

bgs*_*gse 10

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

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

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

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

Android:报警管理器

  • 那么,使用警报管理器方法,您的服务将不需要一直运行.您的服务在执行时唯一需要做的就是检查您的状况X一次,如果是,则发送您的通知.警报管理器将每隔x秒钟负责呼叫您的服务,这与始终运行您的服务基本相同.好处是,你不必担心android系统会杀死你的服务进程,因为它会在每次需要完成任务时重新启动.即使您的应用未运行,因为闹钟管理器不依赖于您的应用. (3认同)

Ham*_*lőd 5

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


Din*_*ati 1

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

从活动中,您可以通过以下方式启动服务

startService(new Intent(activityName.this, serviceName.class));
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)