Android custom notification with buttons and click events and Scaling notification bar

Source: Internet
Author: User

1. Customize a notification layout with buttons: layout_notification;

2. Create notification:

Remoteviews views = new Remoteviews (Getpackagename (), r.layout.layout_nitification); Custom layout views

button click event:

Pendingintent homeintent = Pengdingintent.getbroadcast (this,1,new Intent ("action"), Pengdingintent.flag_update_ Current);

Views.setonclickpendingintent (r.id.btn,homeintent); Click on the ID, click event

To create a notification:

Notificationcompat.builder Mbuilder = new Notificationcompat.builder (getapplicationcontext);

Mbuilder.setcontent (views)//Set layout

. Setongoing (TRUE)//Set whether resident, true for resident

. Setsmallicon (R.mipmap.ic_laucher)//Set Small icons

. Setticker ("Notifications come Up")//Setup Hints

. SetPriority (Notification.priority_max)//Set Priority

. Setwhen (System.currenttimemillis ())//Set display time

. Setcontentintent (Pendingintent.getbroadcast (this,2,new Intent ("Action.view"), pengingintent.flag_update_current    ); Set View Click events

Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Notification_service);

Mnotificationmanager.notify (100,mbuilder.build ())//Display notification: Current Notificationid, current notification

3. Create Notificationbroadcast to receive notifications:

 public class Notificationbroadcast extends Broadcastreceiver {
@Override
public void OnReceive (Context    Context, Intent Intent) {
String action = intent.getaction ();        Action
Collapsestatusbar (context); Close the notification bar
Intent i = new Intent (context,mainactivity.class);
    I.addflags (Intent.flag_activity_new_task);     Must be added to avoid repeatedly opening
if (Action.equals ("Com.xczl.smart.notification.home")) {
I.putextra ("flag", "home"); Value
context.startactivity (i);
}
}

public void Collapsestatusbar (context context) {
    try {
Object Statusbarmanager = Context.getsystemservice ("StatusBar");
Method collapse;
if (Build.VERSION.SDK_INT <= 16) {
Collapse = Statusbarmanager.getclass (). GetMethod ("collapse");
} else {
Collapse = Statusbarmanager.getclass (). GetMethod ("Collapsepanels");
}
Collapse.invoke (Statusbarmanager);
} catch (Exception localexception) {
Localexception.printstacktrace ();
}
}

4. Mainactivity Receive Message:

Overwrite Onnewintent ():

@Override
protected void Onnewintent (Intent Intent) {
String extra = Intent.getstringextra ("flag");
if (! Textutils.isempty (extra)) {
if (Extra.equals ("Home")) {
Specific actions to receive a value
}
}
}

5. Configuration Checklist:

Mainactivity: Set Lanchmode to Singletask

Registration broadcast:

<receiver
Android:name= "Com.xczl.smart.broadcast.NotificationBroadcast"
Android:enabled= "true" >
<intent-filter>
<action android:name= "Action"/>
<action android:name= "Action.view"/>
</intent-filter>
</receiver>

To configure notification bar scaling permissions:

<uses-permission android:name= "Android.permission.EXPAND_STATUS_BAR"/>

      

Android custom notification with buttons and click events and Scaling notification bar

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.