简体   繁体   English

关闭应用程序后服务未运行

[英]Service not running after closing app

I am trying to make background service that will run 15 sec after user closes tha app, I have done service that runs 15 sec (loop with Logs), bud when I close tha app, then it stopes 我正在尝试使后台服务在用户关闭tha应用程序后运行15秒钟,我已经完成了运行15秒钟的服务(使用日志循环),当我关闭tha应用程序时启动,然后停止

and another problem is, when I try to stop it from main activity by stopService(intent) ; 另一个问题是,当我尝试通过stopService(intent)将其从主要活动中停止时; then the onDestroy method is called, but thread with loop continues 然后调用onDestroy方法,但是带有循环的线程继续

.. please can someone help me? ..请有人可以帮我吗?

*sorry for my english - no native :D *对不起我的英语-没有母语:D

public class NotificationService extends Service {
    final private class MyThread implements Runnable {

        int service_id;

        MyThread(int service_id) {
            this.service_id = service_id;
        }


        @Override
        public void run() {

            synchronized (this) {
                for (int i = 0; i < 15; i++) {
                    try {
                        wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.e("onStartCommand", "loop:" + i);
                }
                stopSelf(service_id);
            }
        }
    }

    Thread thread;

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        Log.e("onStartCommand", "started");
        Toast.makeText(this, "onStartCommand", Toast.LENGTH_SHORT).show();

        thread = new Thread(new MyThread(startId));
        thread.start();

        return START_STICKY;
    }


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onDestroy() {
        Log.e("onDestroy", "onDestroy");
        Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
        super.onDestroy();
    }
}

I am trying to make background service that will run 15 sec after user closes tha app, I have done service that runs 15 sec (loop with Logs), bud when I close tha app, then it stopes 我正在尝试使后台服务在用户关闭tha应用程序后运行15秒钟,我已经完成了运行15秒钟的服务(使用日志循环),当我关闭tha应用程序时启动,然后停止

Your code only starts the loop thread when startService(yourNotificationService) is called on the Activity or Broadcast Receiver that is responsible for calling it does so. 您的代码仅在负责调用ActivityBroadcast Receiver的调用startService(yourNotificationService)时启动循环线程。 It then kills itself with stopSelf(service_id) . 然后,它使用stopSelf(service_id)杀死自己。

If, after you have returned from onStartCommand() , you immediately kill the app without calling stopSelf(service_id) (ie your 15 seconds is not up), then your Service will MOST LIKELY restart itself given the START_STICKY return value. onStartCommand()返回后,如果您在不调用stopSelf(service_id)情况下立即stopSelf(service_id)了该应用程序(即,您的15秒还没有结束),那么给定START_STICKY返回值,您的服务将很可能会自行重启。 However, after you call stopSelf(service_id) you are telling the Service to kill itself; 但是,在调用stopSelf(service_id)您正在告诉服务杀死自己。 after you close your app, there is nothing to tell your Service to restart through the onStartCommand() call. 关闭应用程序后,没有任何内容可以告诉您的服务通过onStartCommand()调用重新启动。

and another proble is, when I try to stop it from main activity by stopService(intent); 还有一个问题是,当我尝试通过stopService(intent)将其从主要活动中停止时; then the onDestroy method is called, but thred with loop continues 然后调用onDestroy方法,但继续执行循环

A Service is an Android component; Service是Android组件; it is not another process or thread, it runs in the same process and thread as the main UI thread unless you specify otherwise, as seen here . 这是不是另一个进程或线程,它在相同的进程和线程的主UI线程除非另行指定,因为看到运行在这里

Note that services, like other application objects, run in the main thread of their hosting process. 请注意,服务与其他应用程序对象一样,在其托管过程的主线程中运行。 This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. 这意味着,如果您的服务要执行任何占用大量CPU资源(例如MP3播放)或阻塞(例如网络)的操作,则它应产生自己的线程来执行此工作。 More information on this can be found in Processes and Threads. 有关更多信息,请参见进程和线程。 The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done. IntentService类可作为Service的标准实现提供,该服务具有自己的线程,可以在该线程中安排要完成的工作。

In your case, calling stopService(intent) tells the Service to stop itself, which it does. 在您的情况下,调用stopService(intent)告诉Service自行停止,这样做。 It does not stop the Thread you started (the MyThread instance). 不会停止您启动的ThreadMyThread实例)。 To do that, you must first make your Thread interruptible; 为此,您必须首先使您的Thread中断; see here to do that. 看到这里做到这一点。 Once you do that, you need to change your onDestroy() code to actually interrupt the MyThread instance, as here 完成此操作后,需要更改onDestroy()代码以实际中断MyThread实例,如下所示

@Override
public void onDestroy() {
    Log.e("onDestroy", "onDestroy");
    Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
    thread.interrupt();
    super.onDestroy();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM