-1

I am working on an application where i need to show an activity to user even if the app is closed, for that i am using a Service and from that service i am opening my Activity named OverlayActivity.java.

I am getting the OverlayActivity when the app is either open or is there in task manager, but as soon as i remove the app from task manager, OverlayActivity is not showing up. I am not sure on which step it is not working i.e. whether the service is not working or there is a problem with showing the activity.

I have tried a lot many solutions here on StackOverflow, but couldn't get anything working in my case.

This is the Service class code:

public class OverlayService extends Service {

    private static final String TAG = OverlayService.class.getSimpleName();

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        registerOverlayReceiver();
        return super.onStartCommand(intent, flags, startId);
    }

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

    private void registerOverlayReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(ACTION_DEBUG);
        registerReceiver(overlayReceiver, filter);
        System.out.println("Service aa started");
    }

    private void unregisterOverlayReceiver() {
        unregisterReceiver(overlayReceiver);
    }

    private static final String ACTION_DEBUG = "kunal.lockoverlay.action.DEBUG";

    private BroadcastReceiver overlayReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "[onReceive]" + action);
            if (action.equals(Intent.ACTION_SCREEN_ON)) {
                // ACTON_SCREEN_ON
                showOverlayActivity(context);
            } else if (action.equals(ACTION_DEBUG)) {
                showOverlayActivity(context);
            }
        }
    };

    private void showOverlayActivity(Context context) {
        Intent intent = new Intent(context, OverlayActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}

I am not even sure if what i am trying to do is even possible or not. If at all it is possible where i am doing wrong.

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Kunal
  • 387
  • 3
  • 17

3 Answers3

0

Make a service class and start your service inside onDestroy()

@Override
public void onDestroy() {
   //start service 
    super.onDestroy();
}
Bholendra Singh
  • 161
  • 1
  • 9
0

enter image description here

I guess this is the answer to your question. You just can't do this but what you can do is to show a notification (given that service is still running) to the user which can lead to your required activity. To keep your service alive forever see this post

Asfar Ali
  • 51
  • 3
0

I solved this by providing Autostart permission and without Autostart permission, my service was not able to start once it is closed from the task manager. Now i am able to successfully start the service even after the app is closed from the task manager.

Kunal
  • 387
  • 3
  • 17