Three ways to notify users of Android:
1.Toast Notification
2.Dialog Notification
3.Status bar Notification status bar Notification notification
Sending a status bar notification must use two classes: notificationmanager,notification
1.NotificationManager is a system service that must be obtained via Getsystemservice ()
Notificationmanager notificationmanager= (Notificationmanager) Getsystemservice (Notification_service);
2.Notification is the specific status bar notification object
Call Notificationmanager's Notify () method to create the notification
Two parts:
①: status bar notification
Notification.icon=r.drawable.ic_launcher;
notification.tickertext= "My first notification";
Notification.when=system.currenttimemillis ();
②: drop-down notification list and click to jump:
Two different ways:
One: Setlatesteventinfo () method
Context context = Getapplicationcontext ();
Charsequence contenttitle= "Notification";
Charsequence contenttext= "Notification Context";
Intent intent=new Intent (main.this,turn.class);
Pendingintent pendingintent=pendingintent.getactivity (main.this, 0, intent, 0);
Notification.setlatesteventinfo (context, Contenttitle, ContentText, pendingintent);
Two: Custom notification bar
Notification.flags=notification.flag_auto_cancel; Notify auto-Cancel after user clicks
Set two variables Contentview and contentintent
Remoteviews contenview=new remoteviews (Getpackagename (), r.layout.notification_layout);
Contenview.setimageviewresource (R.id.icon, R.drawable.ic_launcher);
Contenview.settextviewtext (R.id.contenttext, "Custom Notification");
Notification.contentview=contenview;
Intent intent1=new Intent (main.this,turn.class);
Pendingintent pendingintent1=pendingintent.getactivity (main.this, 0, intent1, 0);
Notification.contentintent=pendingintent1;
Tips:
Errors you may encounter: couldn ' t expand remoteviews for:
Check if it is a remoteviews corresponding layout that uses a component that it does not support
Check if the layout file for the remoteviews has a basic error, such as forgetting to declare the width higher
Android status bar notification status bar Notification