Functions and usage of Notification

Source: Internet
Author: User

Click "send notification (standard)" to send the standard notification.

Click "send notification (custom)" to send custom notifications.

Click "Clear all notifications" to clear notifications.

 

 

 

Simply look at the Code:

Activity_main.xml interface 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "android: orientation =" vertical "> <Button android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: text =" Notification (standard) "android: id = "@ + id/btn1"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "send a notification (custom) "android: id =" @ + id/btn2 "/> <Button android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: text = "Clear all notifications" android: id = "@ + id/btn3"/> </LinearLayout>


My. xml code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ProgressBar         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="0"        style="?android:attr/progressBarStyleHorizontal"        android:id="@+id/my_progress"        /></LinearLayout>


MainActivity. java code:

Package com. example. icationicationdemo; import java. util. arrayList; import java. util. random; 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. view. view. onClickListener; import android. widget. button; import android. widget. remoteViews; public class MainActivity extends Activity {/*** Notification, * standard notification * Custom notification * // ids used to save the idArrayList of notifications <Integer> ids = new ArrayList (); int index = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button btn1 = (Button) findViewById (R. id. btn1); Button btn2 = (Button) findViewById (R. id. btn2); Button btn3 = (Button) findViewById (R. id. btn3); btn1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// 1. create a Notification object // parameter 1, which is the notification icon, parameter 2, which is the Notification title, parameter 3, Notification notification = new Notification (R. drawable. ic_launcher, "this is a test notification", System. currentTimeMillis (); // 2. set the notification content subject // PendingIntent is an intent for saving the status // because the notification can be detached from the program (the program ends after it is issued ), so we need an object to maintain the relationship between the notification and the program PendingIntent pending = PendingIntent. getActivity (MainActivity. this, 1, new Intent (MainActivity. this, MainActivity. class), 0); notification. setLatestEventInfo (MainActivity. this, "notification title", "this message is a standard notification", pending); // sets the notification to be cleared. | notification is automatically cleared after being clicked. flags = Notification. FLAG_NO_CLEAR | Notification. FLAG_AUTO_CANCEL; // 3. use the notification manager to send the notification // icationicationmanager is a system service and can be used to send Global notifications notifnotifnotifmanager = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE); Random rd = new Random (); // This id indicates the notification identifier int id = rd. nextInt (); manager. Y (id, notification); ids. add (id) ;}}); // custom Notification btn2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {final notification Notification = new Notification (R. drawable. ic_launcher, "This is a custom notification", System. currentTimeMillis (); RemoteViews rv = new RemoteViews (MainActivity. this. getPackageName (), R. layout. my); notification. contentView = rv; notification. contentIntent = PendingIntent. getActivity (MainActivity. this, 1, new Intent (MainActivity. this, MainActivity. class), 0); notification. flags = Notification. FLAG_NO_CLEAR | Notification. FLAG_AUTO_CANCEL; final NotificationManager manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); manager. Y (1, notification); // start a task and simulate the Progress of new Thread () {public void run () {while (index <100) {notification. contentView. setProgressBar (R. id. my_progress, 100, index, false); index ++; try {sleep (new Random (). nextInt (201) + 300);} catch (Exception e) {e. printStackTrace ();} // update the notification information. If the id is the same, the old information manager is overwritten. Y (1, notification );}}}. start () ;}}); // clear all saved notifications btn3.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {icationicationmanager manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); for (int I = 0; I <ids. size (); I ++) {manager. cancel (ids. get (I ));}}});}}


The comment is quite detailed. If you have any questions, please leave a message.

 

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.