0

I'm creating a screen lock app, and faced notification able to drag down in the lock screen.

I've research it and found this solution to block the notification bar by using addView code below, but I cannot remove the view after user unlocked my app. Please advice how to remove the view.

I've try to use manager.removeView(view); in public void onDestroy() { but it crashed after run this code.

This is the error message

FATAL EXCEPTION: main
Process: com.mehuljoisar.lockscreen, PID: 10256
java.lang.IllegalArgumentException: View=android.view.View{42348cc0 V.ED.... ........ 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:373)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:302)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at com.mehuljoisar.lockscreen.LockScreenActivity.disablePullNotificationTouch(LockScreenActivity.java:261)
at com.mehuljoisar.lockscreen.LockScreenActivity.access$100(LockScreenActivity.java:24)
at com.mehuljoisar.lockscreen.LockScreenActivity$2.onClick(LockScreenActivity.java:113)
at android.view.View.performClick(View.java:4469)
at android.view.View$PerformClick.run(View.java:18788)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5347)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:835)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
at dalvik.system.NativeStart.main(Native Method)

Where should I put the removeView code?

private void disablePullNotificationTouch(String gg) {
  WindowManager manager = ((WindowManager)   getApplicationContext().getSystemService(Context.WINDOW_SERVICE));
  WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
  localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
  localLayoutParams.gravity = Gravity.TOP; //make the line top
  localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |

  // this is to enable the notification to recieve touch events
  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
  // Draws over status bar
  WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

  localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
  localLayoutParams.height = (int) (25 *   getResources().getDisplayMetrics().scaledDensity);
  localLayoutParams.format = PixelFormat.RGBX_8888;
  View view = new View(this);
  if(gg == "on") {
    manager.addView(view, localLayoutParams);
  }else {
    manager.removeView(view);
  }
}

@Override
  public void onCreate(Bundle savedInstanceState) {
    disablePullNotificationTouch("on");
  }
@Override
  public void onDestroy() {
    super.onDestroy();
    disablePullNotificationTouch("off");
  }  
Jasper Jye
  • 81
  • 6

1 Answers1

1

You use

WindowManager manager = ((WindowManager)   this.getSystemService(Context.WINDOW_SERVICE));

Not use

WindowManager manager = ((WindowManager)   getApplicationContext().getSystemService(Context.WINDOW_SERVICE));

Try it!

Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22