Android Popup status bar

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.