Android Status Bar Notification, Android notification
Android notifies users in three ways:
1. Toast Notification
2. Dialog Notification
3. Status Bar Notification, Status Bar Notification
Two types of status bar notifications must be sent: icationicationmanager and Notification.
1. icationicationmanager is a system Service and must be obtained through getSystemService ().
Icationicationmanager NotificationManager = (notificationManager) getSystemService (NOTIFICATION_SERVICE );
2. Notification is the specific status bar Notification object.
Call icationicationmanager's notify () method to create 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 methods:
I. 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 );
Ii. Custom notification bar
Notification. flags = Notification. FLAG_AUTO_CANCEL; the notification is automatically canceled when the user clicks
Set two variables: contentView and contentIntent.
RemoteViews contenView = new RemoteViews (getPackageName (), R. layout. icationication_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:
Possible error: Couldn't expand RemoteViews:
Check whether the components not supported by RemoteViews are used in layout.
Check whether the layout file corresponding to RemoteViews has a basic error, for example, forgetting to declare width or higher.