Since your using the window manager to show the alert view , you need to keep the instance of the view added.
Create a service to add the view to window. So when the service is destroyed , you can remove the view from the window.
Here is a sample code ..
public class FloatingService extends Service {
private WindowManager windowManager;
private View floatingView;
WindowManager.LayoutParams params;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flag, int startId)
{
// add window
showAlert();
return START_STICKY;
}
private void showAlert() {
if(windowManager == null || floatingView == null) {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
floatingView = View.inflate(getBaseContext(),R.layout.floating_layout,null);
// do your stuff to update the view in layout
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.CENTER;
windowManager.addView(floatingView, params);
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatingView != null) windowManager.removeView(floatingView);
}