0

I want to run my service even when the app killed from background task. can anyone give me some suggestion please. (Work fine when I minimize the app)

here my code :

public class MyService extends Service {
    private BroadcastReceiver mReceiver = null;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate() {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            if (ScreenReceiver.wasScreenOn) {
           Log.e("MYAPP", "SCREEN TURNED OFF");

            } else {
            // this is when onPause() is called when the screen state has not changed
        }
        return START_STICKY;   
       }

    @Override
    public void onDestroy() {
            super.onDestroy();
    }
} 
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
help
  • 78
  • 1
  • 2
  • 10
  • You can use a broadcast receiver to detect some intent and launch your service – Mohammad Haidar Oct 06 '16 at 06:52
  • you have example? – help Oct 06 '16 at 06:54
  • 1
    You can send a broadcast in `onDestroy()` of your service and create (and register) a `BroadcastReceiver` to get the `broadcast` if the service is killed. But it´s a really bad practise to restart a service that is killed by the user......you never should do this for an app that´s gonna be published... – Opiatefuchs Oct 06 '16 at 06:58
  • Screen Receiver is my broadcastreceiver – help Oct 06 '16 at 07:14

2 Answers2

2

If your Service is started by your app then actually your service is running on main process. so when app is killed service will also be stopped. So what you can do is, send broadcast from onTaskRemoved method of your service as follows:

public class MyService extends Service {
    private BroadcastReceiver mReceiver = null;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate() {
        final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mReceiver = new ScreenReceiver();
        registerReceiver(mReceiver, filter);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            if (ScreenReceiver.wasScreenOn) {
           Log.e("MYAPP", "SCREEN TURNED OFF");

            } else {
            // this is when onPause() is called when the screen state has not changed
        }
        return START_STICKY;   
       }

    @Override
    public void onDestroy() {
            super.onDestroy();
    }

        @Override public void onTaskRemoved(Intent rootIntent){
              Intent intent = new Intent("com.android.ServiceStopped");
 sendBroadcast(intent);
        } 

Following is your broadcast receiver code

import android.app.*;
import android.content.*;
import android.os.*;

public class ScreenReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent background = new Intent(context, yourservice.class);
        context.startService(background);
    }

}

and have an broadcast receiver which will again start a service. I have tried it. service restarts from all type of kills.

eLemEnt
  • 1,741
  • 14
  • 21
0

You need to restart the service once it gets killed by system or user Hope this will help you

Community
  • 1
  • 1
Moorthy
  • 723
  • 1
  • 6
  • 20