2017-05-21 4 views
0

Je montre un overlay window du service dans mon application. Je veux masquer la fenêtre lorsque press back button. Donc, pour obtenir le back button event que je fais le code suivantRappel du bouton BACK dans la fenêtre Overlay dans Android

class MyView extends MyLayout{ 
    public MyView(Context context){ 
     super(context); 
     LayoutInflater.from(context).inflater(R.layout.my_view,this); 
    } 
} 

Class MyLayout extends FrameLayout{ 
    public MyLayout(Context context){ 
     super(context); 
    } 

    @Override 
    public boolean dispatchKeyEvent(KeyEvent event) { 
     Log.e("key event", "" + event.getKeyCode()); 
     return super.dispatchKeyEvent(event); 
    } 
} 
//service code 
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.MATCH_PARENT, 
       WindowManager.LayoutParams.MATCH_PARENT, 
       WindowManager.LayoutParams.TYPE_PHONE, 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH 
         | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | 
       WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, 
       PixelFormat.TRANSLUCENT); 

final MyView myview = new MyView(this); 
windowManager.addView(myview,param); 

Mais dispatchKeyEvent méthode jamais fait appel à back button pressed. J'ai googlé et j'ai découvert que c'est le moyen de capturer l'événement du bouton de retour. D'une certaine manière, ça ne marche pas dans mon cas. Qu'est-ce que je rate ?

Répondre

0

Si vous voulez juste capturer l'utilisation de l'événement back button pressed:

@override 
public void onBackPressed(){ 
// Do what you need done here 
// ... or pass super.onBackPressed(); 
} 
0

problème réel est le window params. Je dois seulement WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH flags qui empêchent l'autre application à get key events. Donc, le bon code suit

// code de service

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.MATCH_PARENT, 
       WindowManager.LayoutParams.MATCH_PARENT, 
       WindowManager.LayoutParams.TYPE_PHONE, 
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS | 
       WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION | WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, 
       PixelFormat.TRANSLUCENT);