Android notification summary and analysis

Source: Internet
Author: User

Category

There are several types of notification:

1> normal notification

  

1. Content title

2. Large icons

3. Content

4. Additional content information

5. small icons

6. Time

2> large layout Notification

Figure 1

The large layout notification is added after android4.1, while the large layout notification and small layout notification are only'7'Some are different, while others are consistent. The large layout notification displays the large layout only when the top of all notifications are displayed. In other cases, the small layout is displayed. You can also use your fingers to expand it to a large layout (provided that it is a large layout ). For example:

Figure 2

There are three types of notification for large layout: 1 is icationicationcompat. InboxStyle. Figure 2 shows icationicationcompat. BigTextStyle at the left. Figure 2: icationicationcompat. BigPictureStyle

3> custom Layout notification

In addition to the notification provided by the system, we can also customize the notification. As shown in, a music player controls notification:

Figure 3

How to Create notification

   1> instantiate a icationicationcompat. Builder object, such as builder

2> call the relevant methods of builder to set the notification.

    3> call builder. build (). This method returns a notification object.

4> instantiate a icationicationmanager object, for example, manager.

5> call the notify method of manager.

Note:

You do not have to set all the options for a notification, but three items are required:

Small icon, setsetSmallIcon()

Content title, setsetContentTitle()

Content, setsetContentText()

Sample Code

Example program:

0> initialize some code

Copy codeThe Code is as follows: View Code
Public class MainActivity extends Activity implements OnClickListener {

Private static final int icationication_id_1 = 0;
Private static final int icationication_id_2 = 1;
Private static final int icationication_id_3 = 2;
Private static final int icationication_id_4 = 3;
Private static final int icationication_id_5 = 4;
Private static final int icationication_id_6 = 5;
Private static final int icationication_id_7 = 6;
Private static final int icationication_id_8 = 7;

Private static int messageNum = 0;
Private Context context = this;
Private icationicationmanager manager;
Private Bitmap icon;
Private static final int [] btns = new int [] {R. id. btn1, R. id. btn2,
R. id. btn3, R. id. btn4, R. id. btn5, R. id. btn6, R. id. btn7, R. id. btn8,
R. id. btn9 };

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Init ();
}

Private void init (){
// Get Notification Service
Manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE );
// Register the listener
For (int btn: btns ){
FindViewById (btn). setOnClickListener (this );
}

Icon = BitmapFactory. decodeResource (getResources (),
R. drawable. ic_launcher );
}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btn1:
ShowNormal ();
Break;
Case R. id. btn2:
ShowBigView_Text ();
Break;
Case R. id. btn3:
ShowBigView_Pic ();
Break;
Case R. id. btn4:
ShowBigView_Inbox ();
Break;
Case R. id. btn5:
ShowCustomView ();
Break;
Case R. id. btn6:
BackApp ();
Break;
Case R. id. btn7:
BackScreen ();
Break;
Case R. id. btn8:
ShowProgressBar ();
Break;
Case R. id. btn9:
Dismiss ();
Break;
Default:
Toast. makeText (context, "error", Toast. LENGTH_SHORT). show ();
Break;
}
}

Private void dismiss (){
Manager. cancelAll ();
}

1> normal notification
Copy codeThe Code is as follows: View Code
Private void showNormal (){

Notification notification = new NotificationCompat. Builder (context)
. SetLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("showNormal"). setContentInfo ("contentInfo ")
. SetContentTitle ("ContentTitle"). setContentText ("ContentText ")
. SetNumber (++ messageNum)
. SetAutoCancel (true). setDefaults (Notification. DEFAULT_ALL)
. Build ();
Manager. Y (icationication_id_1, notification );
}

2> large layout Text notification
Copy codeThe Code is as follows: View Code
Private void showBigView_Text (){
NotificationCompat. BigTextStyle textStyle = new BigTextStyle ();
TextStyle
. SetBigContentTitle ("BigContentTitle ")
. SetSummaryText ("SummaryText ")
. BigText (
"I am Big textttttttttttttttttttttttttttttttttttttttttttttt !!!!!!!!!!!!!!!!!!!...... ");
Notification notification = new NotificationCompat. Builder (context)
. SetLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("showBigView_Text"). setContentInfo ("contentInfo ")
. SetContentTitle ("ContentTitle"). setContentText ("ContentText ")
. SetStyle (textStyle)
. SetAutoCancel (true). setDefaults (Notification. DEFAULT_ALL)
. Build ();
Manager. Y (icationication_id_2, notification );
}

3> wide-layout Picture-type predicicatio
Copy codeThe Code is as follows: View Code
Private void showBigView_Pic (){
NotificationCompat. BigPictureStyle pictureStyle = new BigPictureStyle ();
PictureStyle. setBigContentTitle ("BigContentTitle ")
. SetSummaryText ("SummaryText"). bigPicture (icon );
Notification notification = new NotificationCompat. Builder (context)
. SetLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("showBigView_Pic"). setContentInfo ("contentInfo ")
. SetContentTitle ("ContentTitle"). setContentText ("ContentText ")
. SetStyle (pictureStyle)
. SetAutoCancel (true). setDefaults (Notification. DEFAULT_ALL)
. Build ();
Manager. Y (icationication_id_3, notification );
}

4> large layout Inbox notification
Copy codeThe Code is as follows: View Code
Private void showBigView_Inbox (){
NotificationCompat. InboxStyle inboxStyle = new NotificationCompat. InboxStyle ();
InboxStyle. setBigContentTitle ("BigContentTitle"). setSummaryText (
"SummaryText ");
For (int I = 0; I <5; I ++)
InboxStyle. addLine ("news:" + I );
Notification notification = new NotificationCompat. Builder (context)
. SetLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("showBigView_Inbox"). setContentInfo ("contentInfo ")
. SetContentTitle ("ContentTitle"). setContentText ("ContentText ")
. SetStyle (inboxStyle)
. SetAutoCancel (true). setDefaults (Notification. DEFAULT_ALL)
. Build ();
Manager. Y (icationication_id_4, notification );
}

5> Custom notification
:

A simple click event is made for the playing button in the middle. (After you click play, close the curtain. Otherwise, the toast prompt may not be displayed)

Copy codeThe Code is as follows: View Code
Private void showCustomView (){
RemoteViews remoteViews = new RemoteViews (getPackageName (),
R. layout. custom_notification );
Intent intent = new Intent (this, tew.usiccontrol. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (context, 0,
Intent, 0 );
RemoteViews. setOnClickPendingIntent (R. id. paly_pause_music,
PendingIntent );
NotificationCompat. Builder builder = new Builder (context );
Builder. setContent (remoteViews). setSmallIcon (R. drawable. music_icon)
. SetLargeIcon (icon). setOngoing (true)
. SetTicker ("music is playing ");
Manager. Y (icationication_id_8, builder. build ());
}

Layout file:Copy codeThe Code is as follows: View Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: gravity = "center_vertical"
Android: orientation = "horizontal">

<ImageView
Android: id = "@ + id/songer_pic"
Android: layout_width = "64dp"
Android: layout_height = "64dp"
Android: src = "@ drawable/yan"/>

<LinearLayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center_vertical"
Android: orientation = "horizontal">

<ImageView
Android: id = "@ + id/last_music"
Android: layout_width = "0dp"
Android: layout_height = "48dp"
Android: layout_weight = "1"
Android: src = "@ drawable/music_previous"/>

<ImageView
Android: id = "@ + id/paly_pause_music"
Android: layout_width = "0dp"
Android: layout_height = "48dp"
Android: layout_weight = "1"
Android: src = "@ drawable/music_play"/>

<ImageView
Android: id = "@ + id/next_music"
Android: layout_width = "0dp"
Android: layout_height = "48dp"
Android: layout_weight = "1"
Android: src = "@ drawable/music_next"/>
</LinearLayout>

</LinearLayout>

Notification with progress barCopy codeThe Code is as follows: View Code
Private void showProgressBar (){

Final NotificationCompat. Builder builder = new NotificationCompat. Builder (
Context );
Builder. setLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("showProgressBar"). setContentInfo ("contentInfo ")
. SetOngoing (true). setContentTitle ("ContentTitle ")
. SetContentText ("ContentText ");
New Thread (new Runnable (){

@ Override
Public void run (){

Int progress = 0;

For (progress = 0; progress <100; progress + = 5 ){
// Set the third parameter of setProgress to true to display the progress bar style without clear progress
Builder. setProgress (100, progress, false );
Manager. Y (icationication_id_7, builder. build ());
Try {
// Sleep for 5 seconds
Thread. sleep (2*1000 );
} Catch (InterruptedException e ){
System. out. println ("sleep failure ");
}
}
Builder. setContentTitle ("Download complete ")
. SetProgress (0, 0, false). setOngoing (false );
Manager. Y (icationication_id_7, builder. build ());

}
}). Start ();
}

Click Event Processing

--------------------------------------------------------------------------------
Sometimes we may need to implement this function: when a new notification appears, we want to click it to directly enter the corresponding interface of the application to view or process the message. Then, when we click the back button, the system returns to the application's main interface instead of the desktop. For example, when we have a new text message, we click it on the taskbar To Go To The read information page. After reading the text message, press "return" to return to the main page of the text message, instead of mobile desktop. To implement such a function, we need to handle it accordingly:

1> return to the main application interface

Copy codeThe Code is as follows: View Code
Private void backApp (){

TaskStackBuilder stackBuilder = TaskStackBuilder. create (this );
// Adds the back stack
StackBuilder. addParentStack (OtherActivity. class );
// Adds the Intent to the top of the stack
Intent resultIntent = new Intent (this, OtherActivity. class );
StackBuilder. addNextIntent (resultIntent );
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent = stackBuilder. getPendingIntent (0,
PendingIntent. FLAG_UPDATE_CURRENT );

Notification notification = new NotificationCompat. Builder (context)
. SetLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("backApp"). setContentInfo ("contentInfo ")
. SetContentTitle ("ContentTitle"). setContentText ("ContentText ")
. SetContentIntent (resultPendingIntent). setAutoCancel (true)
. SetDefaults (Notification. DEFAULT_ALL). build ();
Manager. Y (icationication_id_5, notification );
This. finish ();
}

In the configuration file, we need to configure the OtherActivity that we use to display detailed information as follows:Copy codeThe Code is as follows: <activity
Android: name = "com. example. notification. OtherActivity"
Android: label = "@ string/title_activity_other"
Android: parentActivityName = ". MainActivity">
<Meta-data
Android: name = "android. support. PARENT_ACTIVITY"
Android: value = ". MainActivity"/>
</Activity>

2> directly return to the desktop

Sometimes we may need to implement this function: When we click notification, a slightly larger window is displayed to display the entire message. This window is used to display the entire message content, it has nothing to do with other activities in this application. Then, when we click "back", we will return directly to the mobile desktop. To implement such a function, you only need to call the. setContentIntent method of builder and configure the activity to jump to in the configuration file:

Copy codeThe Code is as follows: View Code
Private void backScreen (){
Intent policyintent = new Intent (this, SpecialActivity. class );
// Sets the Activity to start in a new, empty task
NotifyIntent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK
| Intent. FLAG_ACTIVITY_CLEAR_TASK );
// Creates the PendingIntent
PendingIntent policy_intent = PendingIntent. getActivity (this, 0,
NotifyIntent, PendingIntent. FLAG_UPDATE_CURRENT );

Notification notification = new NotificationCompat. Builder (context)
. SetLargeIcon (icon). setSmallIcon (R. drawable. ic_launcher)
. SetTicker ("backScreen"). setContentInfo ("contentInfo ")
. SetContentTitle ("ContentTitle"). setContentText ("ContentText ")
. SetContentIntent (notify_Intent). setAutoCancel (true)
. SetDefaults (Notification. DEFAULT_ALL). build ();
Manager. Y (icationication_id_6, notification );
This. finish ();
}

Configuration file:Copy codeThe Code is as follows: <activity
Android: name = "com. example. notification. SpecialActivity"
Android: excludeFromRecents = "true"
Android: label = "@ string/title_activity_special"
Android: launchMode = "singleTask"
Android: taskAffinity = "">

Source code download

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.