I am creating a Blue light filter app. So that, I want to display the view over all the apps. I did it by the following Service,
public class OverlayService extends Service {
public OverlayService() {}
View mView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
WindowManager.LayoutParams params;
if (Build.VERSION.SDK_INT < 26)
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
else
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
mView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.overlay, null);
wm.addView(mView, params);
}
@Override
public void onDestroy() {
super.onDestroy();
if (mView != null) {
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
}
}
}
It works fine. But there is only one problem that
"if I remove app from recents, It stops service then automatically starts the service after few seconds"
Why the service is stopped and restarted? How to avoid this?
I tried overriding onStartCommand() and used START_STICKY . But no use.
Help me. Thanks in advance!!!