Android5.x Notification application Parsing
Notification allows us to display the corresponding information on the status bar and screen lock page when we get the message. It is hard to imagine if there is no Notification, our qq and other applications cannot notify us actively, so we need to constantly check the mobile phone to check whether new information and reminders are annoying and reflect the importance of Notification. Here we will introduce three types of Notification: Common Notification, folding Notification, and hanging Notification.
1. Normal Notification
First, create a Builder object and use PendingIntent to control the jump. Jump to the webpage here
Notification.Builder builder = new Notification.Builder(this);Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(http://blog.csdn.net/itachi85/));PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
With builder, we can add various attributes to Notification:
Builder. setContentIntent (pendingIntent); builder. setSmallIcon (R. drawable. lanucher); builder. setLargeIcon (BitmapFactory. decodeResource (getResources (), R. drawable. lanucher); builder. setAutoCancel (true); builder. setContentTitle (general notification );
Finally, create icationicationmanager and call notify:
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(0, builder.build());
To see the effect:
2. Fold-over Notification
Fold-over Notification is a Notification in a custom view. It is used to display long texts and custom la S. It has two States: one is the view in the normal state (if not customized, it is the same as the view style in the preceding normal notification) and the other is the view in the expanded state. Different from normal Notification, we need a custom view, and the process displayed in this view is no longer a process as we create the view, so we need to use RemoteViews, first, we need to use RemoteViews to create our custom views:
// Use RemoteViews to create a custom Notification View RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. view_fold );
View layout file:
We need to assign the custom View to the Notification view. The following Code assigns the custom View to the view when the Notification is expanded.
// Specify the view notification. bigContentView = remoteViews when expanding;
Of course, we can also assign a custom View to the view in the normal state of Notification.
// Specify the view notification. contentView = remoteViews in normal state;
There is no difference between other code and common Notification. The complete code of folding Notification is as follows:
Notification. builder builder = new Notification. builder (this); Intent mIntent = new Intent (Intent. ACTION_VIEW, Uri. parse (http://blog.csdn.net/itachi85/); PendingIntent pendingIntent = PendingIntent. getActivity (this, 0, mIntent, 0); builder. setContentIntent (pendingIntent); builder. setSmallIcon (R. drawable. foldleft); builder. setLargeIcon (BitmapFactory. decodeResource (getResources (), R. drawable. lanucher); builder. setAutoCancel (true); builder. setContentTitle (foldable Notification); // use RemoteViews to create a custom Notification View RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. view_fold); Notification notification = builder. build (); // specify the view notification when expanding. bigContentView = remoteViews; icationicationmanager. Y (1, notification );
If it is not a custom normal state view, there is no difference between a foldable Notification and a common Notification.
Let's pull down, so that the fold-over Notification is fully expanded, and our custom view will appear.
3. Hanging Notification
Hanging Notification is a newly added method of android5.0. Unlike the previous two display methods, the first two display methods need to pull down the Notification bar to see the Notification, the Hanging Notification does not need to be displayed in the drop-down Notification bar directly on the top of the screen and the focus remains unchanged on the user's interface. Therefore, the user's operations will not be interrupted and will disappear automatically in a few seconds.
Unlike the previous two types of Notification, he needs to call setFullScreenIntent to change Notification to hanging Notification.
// If the description PendingIntent already exists, the current PendingIntent hangPendingIntent = PendingIntent will be canceled before a new Intent is generated. getActivity (this, 0, hangIntent, PendingIntent. FLAG_CANCEL_CURRENT); builder. setFullScreenIntent (hangPendingIntent, true );
Complete code for implementing hanging Notification:
Notification. builder builder = new Notification. builder (this); Intent mIntent = new Intent (Intent. ACTION_VIEW, Uri. parse (http://blog.csdn.net/itachi85/); PendingIntent pendingIntent = PendingIntent. getActivity (this, 0, mIntent, 0); builder. setContentIntent (pendingIntent); builder. setSmallIcon (R. drawable. foldleft); builder. setLargeIcon (BitmapFactory. decodeResource (getResources (), R. drawable. lanucher); builder. setAutoCancel (true); builder. setContentTitle (hanging notification); // set to jump to Intent hangIntent = new Intent (); hangIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); hangIntent. setClass (this, MyNotificationActivity. class); // if the description PendingIntent already exists, the current PendingIntent hangPendingIntent = PendingIntent will be canceled before a new Intent is generated. getActivity (this, 0, hangIntent, PendingIntent. FLAG_CANCEL_CURRENT); builder. setFullScreenIntent (hangPendingIntent, true); icationicationmanager. notify (2, builder. build ());
To see the effect
4. Display level of Notification
Android5.0 adds a new mode Notification display level. There are three display levels:
VISIBILITY_PUBLIC will only display notification VISIBILITY_PRIVATE when no screen lock is available. notification will be displayed in any situation.
VISIBILITY_SECRET displays notifications when the security lock and no lock screen are available
The setting is very simple. You only need to call the setVisibility method.
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
Here I have written a method to set the Notification level. radioGroup is used to demonstrate the display levels of Notification. For details, refer to the source code.
private void selectNotofovatiomLevel(Notification.Builder builder) { switch (radioGroup.getCheckedRadioButtonId()) { case R.id.rb_public: builder.setVisibility(Notification.VISIBILITY_PUBLIC); builder.setContentText(public); break; case R.id.rb_private: builder.setVisibility(Notification.VISIBILITY_PRIVATE); builder.setContentText(private); break; case R.id.rb_secret: builder.setVisibility(Notification.VISIBILITY_SECRET); builder.setContentText(secret); break; default: builder.setVisibility(Notification.VISIBILITY_PUBLIC); builder.setContentText(public); break; } }