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.