When we use the mobile phone, if the text message, and we do not click to view, is not in the top of the mobile phone status bar has a small message icon hint AH. Do you want to implement this function as well? Today's notification is to solve this problem.
We also know that the Android system is also escalating, there are many ways to use notification, and some methods have been discarded by Android, and now I have implemented three different methods and adapted to different versions of Android. Now I will publish the code, I like to write the explanation in the code, here I do not say, first look at the effect of the picture:
Look at the code again, the main code is as follows:
[Java] View plain copy package net.loonggg.notification; import android.app.activity; import android.app.notification; import android.app.notificationmanager; 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 NOTIFICATION_FLAG = 1; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); &Nbsp; setcontentview (r.layout.activity_main); } public void notificationmethod (View view) { // on Android for notification processing, First you need to reset the system where to get the Notification Manager Notificationmanager, which is a system service. NotificationManager manager = ( Notificationmanager) getsystemservice (context.notification_service); switch (View.getid ()) { // Default Notification case R.id.btn1: // Create a pendingintent, Similar to the intent, the difference is not immediately called, the need to start the activity in the Drop-down State bar, so the use of pendingintent, that is, click Notification Jump to boot to which Activity PendingIntent Pendingintent = pendingintent.getactivity (this, 0, new intent (This , mainactivity.class), 0); // compatible with android 2.x version is the way to handle // notification notify1 = new notification (R.drawable.message , // "Tickertext:" + "You have new short message, please note check." ", system.currenttimemillis ()); notification notify1 = new notification (); notify1.icon = r.drawable.message; notify1.tickerText = " Tickertext: You have new short message, please check it carefully. "; notify1.when = system.currenttimemillis (); notify1.setlatesteventinfo (this, "Notification title", "This is the notification message ", pendingintent); notify1.number = 1; notify1.flags |= notification.flag_auto_cancel ; flag_auto_cancel indicates that the notification will be cleared when the notification is clicked by the user. // to initiate notification by Notification Manager. If the ID is different, add a hint to statu for each click