Background
In the launcher of making an Android TV box, one of the requirements is to click on the icon and then pop up the notification bar.
Realize
After reviewing the information, we learned that the processing of the notification bar is related to a class called Statusbarmanager, but the class is not intended for developers, but is used by the Android system itself, that is, it does not provide an interface for developers to use. See on the web that someone uses the reflection mechanism to implement, the code is as follows:
Public void shownotification () { try { = mainactivity. this. Getsystemservice ("StatusBar"); if NULL { = Service.getclass (). GetMethod ("expand"); Expand.invoke (service); } Catch (Exception e) { e.printstacktrace (); }}
But after running the code directly, I found that it would throw an exception, and then went to look at the source code of Statusbarmanager and found that the class had no expand method at all. But there is a method called Expandnotificationspanel that can achieve this effect. Then the code was modified, as follows:
Public voidshownotification () {Try { /** Using the reflection mechanism to get the Statusbarmanager object that the system is running, and then by getting its method to invoke the popup*/Object Service= Mainactivity. This. Getsystemservice ("StatusBar"); if(Service! =NULL) {Method expand= Service.getclass (). GetMethod ("Expandnotificationspanel"); Expand.invoke (service); } } Catch(Exception e) {e.printstacktrace (); }}
I thought this was the right time, but after running the exception is thrown, after viewing the original is to use this class is required to add permissions. Need to add in Androidmanifest.xml
<android:name= "Android.permission.EXPAND_STATUS_BAR"/>
Done! The following are the operations:
Figure 1 before clicking
Figure 2 Post-click
Project DOWNLOAD Link: http://download.csdn.net/detail/u010662742/8126165
Android Popup status bar