Android Initial learning-the use of notification method __java

Source: Internet
Author: User

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   

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.