I need to dismiss system alert window on back
pressed and home
button event.I have tried with onKeyEvent
but in vain. As we can't capture the back
pressed event in a service, how to achieve this?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Since it's a service that hosting an overlay window, It's a bit tricky solution but it is possible.
You should handle these 2 cases separately (overriding home button press, and back button press).
1. Overriding home button press:
Create this HomeWatcher class which contains a BroadcastReceiver that will notify when home button was pressed. Register this receiver only when your window comes up.
Android: associate a method to home button of smartphone
Inside your service onCreate method use this:
2. Overriding back button press:
The idea is creating an empty layout as a data member of your window class, and attach your view to it (even if its an inflated XML layout).
For example, this is gonna be your window class:
And now create the MyLayout class to override the back button press:
I know it's a bit complicated as I said since it's a system alert window hosted by a service, BUT it's working. I have the same issue as well and it has been solved exactly like that. Good luck.
Implement the code to detect easily Back Button or Home Button pressed.
Note:
Add this Permission in Android Manifest to show the alert Window .
Happy Coding :)
I understand that you are using the
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
permission for showing a floating view.Using a floating view you can intercept the
back
button press, but thehome
button press cannot be intercepted (android won't let you primarily because of security reasons).To intercept the
back
button press you need to add a wrapper when you inflate your floating view. Your wrapper should look like this:Then you add it to your floating view as a root:
My complete code looks like this:
Define a custom layout and override
dispatchKeyEvent
, for example:Then add the view with this code:
When you press the back button the view will dismiss.
Declare this on your activity. super.OnBackPressed automatically calls back method in android. it will surely cancel your dialog.
in addition, your dialog must look like this.
or you can set Negative button..
Hope this helps!
You need to overwrite the
onBackPressed
method.