J'essaye de créer une fenêtre de superposition mais quand j'essaye d'ajouter la vue au WindowManager, cela me donne une exception. J'ai ajouté l'autorisation "SYSTEM_ALERT_WINDOW" et j'ai activé "Dessiner sur d'autres applications" dans l'info de l'application. J'appelle ceci à partir de la fonction onCreate d'un service.WindowManager.addView() provoque BadTokenException
- Appareil: Emulator en cours d'exécution 8.0.0
- SDK cible et des versions compilées SDK: 26
- Version AppCompat: 26.0.0
code:
WindowManager manager = (WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout overlay = (RelativeLayout) inflater.inflate(R.layout.button_main, null);
final WindowManager.LayoutParams params =
new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_PANEL,
0,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.START;
params.x = 0;
params.y = 0;
manager.addView(overlay, params);
Exception stacktrace :
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:764
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
Peu importe ce que TYPE I utilise pour les LayoutParams, je reçois toujours ce plantage.
Merci. C'était ça! Je suis en train de le faire dans un service d'accessibilité et je me demande pourquoi TYPE_ACCESSIBILITY_OVERLAY ne fonctionne pas. (avec le service activé en tant que service d'accessibilité connecté dans les paramètres) – ravindu1024