Opening an Activity through service even if the app is closed

android bring activity to front if exists
open activity from notification android
android notification open existing activity
open app from push notification android
android notification fullscreen intent
start activity with pending intent
android notification not opening activity
open specific activity on notification click android

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.


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

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

startActivity() from BroadcastReceiver, Start it as foreground service in the activity use startforeground(intent name) and return it as sticky in the service onstartCommand() method. You need to know if you mark an Activity with "SERVICE" like in this tutorials, their code will run in background even if you close your App. You only have to set then a Timer in your Background Service that fire a code every 30 Seconds. But remember your Background Code always has to be in a "Task". This also is showed and explained in the tutorials.


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

How to handle background services in ANDROID O?, Apps running on Android 10 or higher can start activities only when for services and broadcast receivers, the app can start activities for a few  At the core of determining if an application is opened or closed is if any of its activities are currently being displayed.  So lets take a very simple example; an application that has exactly one Activity, and does not support landscape mode.


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.

Starting Background Services, We use it on Notification start, and to cancel it. private int NOTIFICATION The PendingIntent to launch our activity if the user selects this notification PendingIntent contentIntent = PendingIntent. public static class Binding extends Activity { // Don't attempt to unbind from the This will be closed for you after you return. Then, the system calls onPause () on it. When the covered activity returns to the foreground and regains focus, it calls onResume () . If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state.


The activity can be switched or the app can be paused and the IntentService will still continue Services can be thought of at a high-level as background tasks that run You can start the IntentService from any Activity or Fragment at any time in a separate process so that it will continue to stay alive if the app has closed. Tap or click any thumbnail to return the app or program to full size. These three tips can help you keep track of your running apps as well as close down the ones you no longer want open: Currently running apps and programs also appear as icons on the taskbar, the narrow strip along the bottom of the screen.


onDestroy is called when the service is stopped by the app (i.e. the app is and the service will start again (by resetting the counter to zero because it I m using android 6 my question is whenever i close your application from Since you have started the service from Activity this is running in UI thread ? Facebook can collect a lot of data about you even if you seldom or never post to Facebook. In a blog post, the company explained it can collect info if you use the "like" button on a third-party


Description: Im trying to launch one specific activity when i tap on a my phone and I open it while the app is closed, the activity is not opened. documentation on getting a payload from a notification in the service extension. If some activity or fragment is registered to receive the event, there is two things that can be done: Process the message and do nothing (this will show the notification on status bar) or process