Android implements message notification via Notification&notificationmanager _android

Source: Internet
Author: User
Tags xmlns

Notification is an alert user that lets your application run without opening or running in the background. It is an invisible program component (broadcast Receiver,service and inactive activity) that alerts users to the best way to take notice of events.

  1, a new Android project

I create a new project minsdkversion= "One", targetsdkversion= "19". Which is to support the minimum version of 3.0.

  2, habitually open the project manifest file Androidmanifest.xml, add a permission: <uses-permission android:name= "Android.permission.VIBRATE" /> Do not add the impossible.

  3, add a few buttons in the layout Activity_main.xml , look like this, vertical typesetting linearlayout

Specific code

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android: paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android:o rientation= "Vertical" tools:context= ". Mainactivity "> <button android:id=" @+id/btn_01 android:layout_width= "Match_parent" Android:
    layout_height= "Wrap_content" android:text= "3.0 previous versions of the notification, with a new bar" android:onclick= "click"/> <button android:id= "@+id/btn_02" android:layout_width= match_parent "android:layout_height=" Wrap_con Tent "android:text=" large visual text Notice "android:onclick=" click "/> <button android:id=" @+id/btn_03 "Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Large View picture Notification" android:onclick= "click" /> <button android:id= "@+id/btn_04" android:layout_width= "Match_parent" Android:layout_heig ht= "Wrap_content" android:text= "progress bar Notification" android:onclick= "click"/> </LinearLayout>

4, the code in the mainactivity:

 Package Com.xin.day__notificationdemo;
  Import Java.util.Timer;
  Import Java.util.TimerTask;
  Import android.app.Activity;
  Import android.app.Notification;
  Import Android.app.NotificationManager;
 Import android.app.PendingIntent;
 Import android.content.Intent;
 Import Android.graphics.BitmapFactory;
 Import Android.os.Bundle;
 Import Android.support.v.app.notificationcompat;
 Import Android.support.v.app.notificationcompat.bigpicturestyle;
 Import Android.support.v.app.notificationcompat.bigtextstyle;
 Import Android.support.v.app.notificationcompat.builder;
 Import Android.util.Log;
 Import Android.view.View;
   public class Mainactivity extends the unique identifier of the activity {//notification, different notifications to be distinguished in one application private static final int NO = x;
   private static final int NO = x;
   private static final int NO = x;
   private static final int NO = x;
   Progress bar to use private int progress =; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
     Setcontentview (R.layout.activity_main); //click method, and the value of the OnClick property of each button in the XML file to be consistent public void click (View view) {//create Notificationmanager Final Notif
     Icationmanager manager = (Notificationmanager) getsystemservice (Notification_service); Control four Control switch (View.getid ()) {case R.id.btn_: {Notification Notification = new Notification (
       );
       Notification.icon = R.drawable.ic_launcher; Notification.tickertext = "There is news ...
       ";
       Intent Intent = new Intent (this, mainactivity.class);
       Pendingintent pendingintent = Pendingintent.getactivity (this,, intent, pendingintent.flag_update_current);
       Notification.setlatesteventinfo (This, ". Previous notice", "Try it only", pendingintent);
       Notification.when = System.currenttimemillis ();
       Notification.defaults = Notification.default_all;
       Notification.flags = Notification.flag_auto_cancel;
       Notification.number =;
   Notification.vibrate = new long[]{,};    Manager.notify (NO, notification);
     } break; Case r.id.btn_:{//Large view text notice//Create Message Builder in expansion pack notificationcompat.builder Builder = new Notificationcompat .
       Builder (this);
       Set when message is prompted, icon and hint text Builder.setsmallicon (r.drawable.ic_launcher). Setticker ("New News");
       Need style Bigtextstyle style = new Bigtextstyle (); Style.setbigcontenttitle ("class notice")//Notice title Style.bigtext ("JSP on full B this afternoon");//Notification text content//Large view text specific content style.se
       Tsummarytext ("This is the normal course arrangement, please students on Time class");
       Builder.setstyle (style);
       Displays the time when the message arrived, setting the current time Builder.setwhen (System.currenttimemillis ());
       Gets a notification object Notification Notification = Builder.build ();
       Notification.flags = Notification.flag_auto_cancel; Send (show) notification//notify () the first parameter ID, identifier for this notification unique within your application? meaning that this
     Notice the unique identifier manager.notify (NO, notification) in your application;
     } break;
   Case r.id.btn_:{    Large view Picture notice notificationcompat.builder builderpic = new Builder (this);
       Builderpic.setsmallicon (R.drawable.ic_launcher). Setticker ("Sina Sports Reminder");
       To set Bigpicturestyle Picturestyle = new Bigpicturestyle ();
       Picturestyle.setbigcontenttitle ("Sina sports fast Ship vs Knight");
       Picturestyle.bigpicture (Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_game)); Picturestyle.setsummarytext ("Fast Ship vs Knight King Mountain Battle!!!")
       /Do not care about the text//Set style Builderpic.setstyle (Picturestyle);
       Set the display time Builderpic.setwhen (System.currenttimemillis ());
       Notification Notification = Picturestyle.build ();
       Notification.flags = Notification.flag_auto_cancel;
     Manager.notify (NO, notification);
     } break; Case r.id.btn_:{//progress bar informs final Notificationcompat.builder builderprogress = new Notificationcompat.builder (t
       His);
       Builderprogress.setsmallicon (R.drawable.ic_launcher). Setticker ("Progress bar Notice"); Builderprogress.sEtprogress (, progress, false);
       Final Notification Notification = Builderprogress.build ();
       Send a notification manager.notify (NO, notification);
       Creates a timer timer = new timer ();
           Timer.schedule (New TimerTask () {@Override public void run () {LOG.I ("Progress", progress+ "");
             while (progress <=) {progress + +;
             try {thread.sleep ();
             catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
             }//Update progress bar builderprogress.setprogress (, progress, false);
           Notify Manager.notify again (NO, Builderprogress.build ());
           }//Timer exit This.cancel ();
           The progress bar exits Manager.cancel (NO);
     return;//End Method}},);
     } break;
     Default:break; }
   }
 }

5, run: My virtual machine version is 4.0 (API19), press and hold the notice left (right) slide can let the notice hour.

The effect is as follows:


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.