When we use a mobile phone, if we receive a text message without clicking to view it, is there a small text message icon prompt in the top status bar of the mobile phone? Do you also want to implement this function? Today's Notification solves this problem.
We also know that the Android system is constantly being upgraded, and there are many ways to use Notification. Some methods have been abandoned by android. Now I have implemented three different methods, and adapt to different android versions. Now I will publish the code. I like to write the explanation in the code. I will not talk about it here. Let's take a look at it first:
Let's look at the code again. The main code is as follows: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cHJlIGNsYXNzPQ = "brush: java;"> package net. loonggg. notification; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. context; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. remoteViews; public class MainActivity extends Activity {Private static final int icationication_flag = 1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);} public void notificationMethod (View view) {// handle notifications in Android. First, you need to obtain the NotificationManager from the system, which is a system Service. Icationicationmanager manager = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE); switch (view. getId () {// default notification case R. id. btn1: // create a PendingIntent, which is similar to the Intent. The difference is that the PendingIntent is used because it is not called immediately and needs to start from the pull-down status bar, click Notification to jump to which ActivityPendingIntent pendingIntent = PendingIntent. getActivity (this, 0, new Intent (this, MainActivity. class), 0); // The following must be compatible with Android 2. processing Method of Version x // using ICA Tion messages Y1 = new Notification (R. drawable. message, // "TickerText:" + "you have a new short message. Check it carefully! ", System. currentTimeMillis (); Notification history Y1 = new Notification (); policy1.icon = R. drawable. message; policy1.tickertext =" TickerText: You have a new short message. Please check it! "; Policy1.when = System. currentTimeMillis (); policy1.setlatesteventinfo (this, "Notification Title", "This is the notification message", pendingIntent); policy1.number = 1; policy1.flags | = Notification. FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL indicates that the notification will be cleared when the notification is clicked by the user. // Initiate a notification through the notification manager. If the IDs are different, a prompt manager is added to statu for each click. Y (icationication_flag, notify1); break; // notification API11 by default and available case R later. id. btn2: PendingIntent pendingIntent2 = PendingIntent. getActivity (this, 0, new Intent (this, MainActivity. class), 0); // use Notification. create a Notification by using the Builder. Note that Notification using y2 = new Notification is supported only after API Level // API11. builder (this ). setSmallIcon (R. drawable. message) // set the small image in the status bar. The size is generally 24 × 24. This image is also in the drop-down status bar. If you need to change a larger image, you can use setLargeIcon (Bitmap/icon). setTicker ("TickerText:" + "to view new messages! ") // Set the prompt text displayed on the status // bar. setContentTitle ("Notification Title") // set the Activity after the drop-down status // bar. In this example, the Title displayed in the TextView of NotififyMessage. setContentText ("This is the notification message") // detailed content displayed in TextView. setContentIntent (pendingIntent2) // associate PendingIntent. setNumber (1) // The number displayed on the Right of TextView. You can enlarge the image 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 .. GetNotification (); // note that build () is added after API level/16 and later. In API11, you can use getNotificatin () to replace policy2.flags | = Notification. FLAG_AUTO_CANCEL; manager. Y (icationication_flag, ikey2); break; // notification API16 by default and available case R later. id. btn3: PendingIntent pendingIntent3 = PendingIntent. getActivity (this, 0, new Intent (this, MainActivity. class), 0); // use Notification. builder to create notifications. Note that Notification policy3 = ne is supported only after API Level // API16 W Notification. Builder (this). setSmallIcon (R. drawable. message). setTicker ("TickerText:" + "you have a new short message, please check it! "). SetContentTitle ("Notification Title "). setContentText ("This is the notification message "). setContentIntent (pendingIntent3 ). setNumber (1 ). build (); // note that build () is added after API // level16 and later. API11 can use getNotificatin () to replace policy3.flags | = Notification. FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL indicates that the notification will be cleared when the notification is clicked by the user. Manager. icationy (icationication_flag, notify3); // Step 4: Initiate a notification through the notification manager. If the id is different, a break prompt is added to the status for each click. // custom notification case R. id. btn4: // Notification mydomainy = new Notification (R. drawable. message, // "Custom notification: You have a new short message. Check it carefully! ", System. currentTimeMillis (); Notification mydomainy = new Notification (); mydomainy. icon = R. drawable. message; mypolicy. tickerText = "TickerText: You have a new short message. Check it carefully! "; Mypolicy. when = System. currentTimeMillis (); mypolicy. flags = Notification. FLAG_NO_CLEAR; // The RemoteViews rv = new RemoteViews (getPackageName (), R. layout. my_notification); rv. setTextViewText (R. id. text_content, "hello wrold! "); Mypolicy. contentView = rv; Intent intent = new Intent (Intent. ACTION_MAIN); PendingIntent contentIntent = PendingIntent. getActivity (this, 1, intent, 1); myevery y. contentIntent = contentIntent; manager. Y (icationication_flag, myNotify); break; case R. id. btn5: // clear the notification manager whose id is icationication_flag. cancel (icationication_flag); // clear all notifications // manager. cancelAll (); break; default: break ;}}}Look at the main layout file again:
Another is the layout file my_icationication.xml of the custom notification. The Code is as follows:
Reprinted please indicate the source: http://blog.csdn.net/loongggdroid/article/details/17616509