2

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

and another problem is, when I try to stop it from main activity by stopService(intent); then the onDestroy method is called, but thread with loop continues

.. please can someone help me?

*sorry for my english - no native :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();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
legend27
  • 21
  • 2
  • how are you making sure the Service is being started just as the app is closed? – Mercato Nov 25 '17 at 13:30
  • for now I am calling the service by pressing button, latter the app will play short video and the close itself ( not sure how exactly I am going to do this :D ) – legend27 Nov 25 '17 at 13:42
  • please see my answer below, clicking a button only works if the app is not closed... – Mercato Nov 25 '17 at 13:47
  • Possible duplicate of [Android Service Stops When App Is Closed](https://stackoverflow.com/questions/16651009/android-service-stops-when-app-is-closed) – ADM Nov 25 '17 at 14:28

1 Answers1

0

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

Your code only starts the loop thread when startService(yourNotificationService)is called on the Activity or Broadcast Receiverthat is responsible for calling it does so. It then kills itself with stopSelf(service_id).

If, after you have returned from onStartCommand(), you immediately kill the app without calling stopSelf(service_id) (i.e. your 15 seconds is not up), then your Service will MOST LIKELY restart itself given the START_STICKY return value. However, after you call stopSelf(service_id) you are telling the Service to kill itself; after you close your app, there is nothing to tell your Service to restart through the onStartCommand() call.

and another proble is, when I try to stop it from main activity by stopService(intent); then the onDestroy method is called, but thred with loop continues

A Service is an Android component; 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.

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. 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.

In your case, calling stopService(intent) tells the Service to stop itself, which it does. It does not stop the Thread you started (the MyThread instance). To do that, you must first make your Thread interruptible; see here to do that. Once you do that, you need to change your onDestroy() code to actually interrupt the MyThread instance, as here

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