Android Study Notes (May 4): Notification (Part 1)

Source: Internet
Author: User

For services running in the background, some methods are required to notify users, such as notifying users of incoming calls and notifying users of new messages. Such notifications are displayed on the status bar and can contain hardware reminders, such as vibration, flashing LED light, and playing sound. In Android, you can use icationicationmanager to initiate a notification.

Let's take a look at a simple example, as shown in the figure below. The interface is very simple. There are two big buttons, the above is the departure notification, and the following is the cancellation notification. After a notification is triggered, the status bar displays the icon and notification Summary (ticker, the stock market's automatic recorder). After a few seconds, there will be only one piece left, this displays the title and text of the content in TextView mode. Click notification to jump to an activity.

Create an activity that starts after clicking the notification, in simple mode and in TextView Mode

Public class policymessage extends Activity {

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TextView t = new TextView (this );
T.. setText ("This is the message! ");
SetContentView (t );
}

}

The following is the notification processing program.

Public class policytest extends Activity {
PrivateIcationicationmanagerMgr = null; // use the notification manager to implement notification operations
Int count = 0; // sometimes the service sends multiple notifications. We use a counter to simulate different notifications.
Private static final intNOTIFY_ME_ID= 1337;
// The Notification Manager uses the notification ID to identify different notifications. To create or delete a notification, you must provide a notification number.

Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. policy_test );
// Step 1: process notifications in Android. First, you need to obtain the notification manager NotificationManager from the system, which is a system Service. 
Mgr =(Icationicationmanager)GetSystemService (NOTIFICATION_SERVICE);
}

/* Press the button above to start notifyMe ()*/
Public void policyme (View v ){
Count ++; // This is the notification serial number. Every time you press the button, a new notification is simulated.
// Step 2: Create a PendingIntent, which is similar to Intent. The difference is that, because it is not called immediately, the drop-down status bar must be the starting activity, so PendingIntent is used.
PendingIntent I = PendingIntent. getActivity (This, 0, new Intent (this, policymessage. class), 0);
// Step 3: Create a Notification using Notification. Builder. Note that this is only supported after API Level 11. to be compatible with Android 2. x, you can refer to the Code in the comment below.
Notification mypolicy
= NewNotification. Builder (this)
.SetSmallIcon(R. drawable. note)
// Set a small image in the status bar. The size is generally 24 × 24. This image is also displayed in the drop-down status bar. If you need to change a larger image, you can use setLargeIcon (Bitmap icon)
.SetTicker("Ticker:" + count) // sets the prompt text displayed on the status bar.
.SetContentTitle("Title:" + count) // you can specify the title of the activity after the status bar drop-down.
.SetContentText("Notification text" + count) // detailed content displayed in textview
.SetContentIntent(I)
// Associate pendingintent
.SetNumber(Count)
// The number displayed on the right of the TextView, which can be enlarged to the right. This number also has a serial number left or right. If multiple notifications are triggered (with the same ID), you can specify which one to display.
.Build();
// Note that build () is added at API level 16 and can be replaced by getNotificatin ().
/* Compatible with Android 2.x versions.
Notification mypolicy = new Notification(R. drawable. note, "Ticker:" + count, System. currentTimeMillis ());
Mypolicy.SetLatestEventInfo(This, "Notification Title", "This is the notification message", I );
Mypolicy.Number= Count ;*/

Mypolicy. flags | = Notification. FLAG_AUTO_CANCEL;
// FLAG_AUTO_CANCEL indicates that the notification will be cleared when the notification is clicked by the user.
Mgr. Sort y (NOTIFY_ME_ID,Mypolicy);// Step 4: Use the notification manager to initiate a notification.If the IDs are different, a prompt is added to the status field for each click.
}

/* The following button triggers the elimination notification.
*/
Public void policyclear (View v ){
Mgr. cancel (NOTIFY_ME_ID);
}

}

If the notification still requires a hardware reminder, such as vibration, you can handle it as follows:

Note. setVibrate (new long [] {500L, 200L, 200L, 500L })
// Note. vibrate = new long [] {500L, 200L, 200L, 500L };

Notification. Builder provides methods such as setLights () and setSound (), and many others. You can find them in reference based on your needs.

Related links:
My Android development articles

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.