Notification Notification Bar
is a message displayed in the status of a phone that represents a global effect notification
The quick steps to create a notification can be divided into the following four stages:
First step : Get Notificationmanager object by Getsystemservice () method;
The second step : To set some properties of notification such as: content, icon, title, the corresponding notification action to deal with, and so on;
The third step : Execute a notification by the Notify () method of the Notificationmanager object;
Fourth Step : Cancel a notificatioin with the Cancel () method of the Notificationmanager object;
Example:
Layout:
1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent"5android:orientation="Vertical">6 7<Button8Android:id="@+id/notification_open"9Android:layout_width="fill_parent"Tenandroid:layout_height="wrap_content" Oneandroid:text="Open the notification bar"/> A -<Button -Android:id="@+id/notification_close" theAndroid:layout_width="fill_parent" -android:layout_height="wrap_content" -android:text="Cancel Notification Bar"/> - +</LinearLayout>
Layout
Java files:
1 Package information;2 3 4 5 import Android.annotation.SuppressLint;6 import android.app.Activity;7 import android.app.Notification;8 import Android.app.Notification.Builder;9 import Android.app.NotificationManager;Ten import android.app.PendingIntent; One import Android.content.Context; A import android.content.Intent; - import Android.os.Bundle; - import Android.view.View; the import Android.view.View.OnClickListener; - import Android.widget.Button; - - import COM.EXAMPLE.ALLCODE.R; + - Public classNotification_text_one extends Activity implements onclicklistener{ +Notificationmanager manger;//notification Control class A intnotification_id; at PrivateButton Open; - PrivateButton Close; - @Override - protected voidonCreate (Bundle savedinstancestate) { - //TODO auto-generated Method Stub - super.oncreate (savedinstancestate); in Setcontentview (r.layout.notification); - toOpen =(Button) Findviewbyid (r.id.notification_open); +Close =(Button) Findviewbyid (r.id.notification_close); - //System Services theManger =(Notificationmanager) Getsystemservice (context.notification_service); * $Open.setonclicklistener ( This);Panax NotoginsengClose.setonclicklistener ( This); - } the @Override + Public voidOnClick (View v) { A //TODO auto-generated Method Stub the Switch(V.getid ()) { + CaseR.id.notification_open://Open the notification bar - sendnotification (); $ $ Break; - CaseR.id.notification_close: - Manger.cancel (notification_id); Cancel Notification Bar the - Break;Wuyi the default: - Break; Wu } - } About $ Private voidsendnotification () { -Intent Intent =NewIntent ( This, Alertdialog_text.class); - -pendingintent pi = pendingintent.getactivity ( This,0, Intent,0); ABuilder Builder =NewNotification.builder ( This); +Builder.setsmallicon (r.drawable.icon_72);//set the notification bar icon theBuilder.setticker ("Hello");//set notification bar prompt -Builder.setwhen (System.currenttimemillis ());//Set the time $Builder.setcontenttitle ("This is the notification bar header");//Notification Bar Header theBuilder.setcontenttext ("here is the notification bar content");//Notification Bar Contents theBuilder.setcontentintent (PI);//set the intention after the click the //effect, you need to add the appropriate permissions theBuilder.setdefaults (Notification.default_sound);//Set Prompt Sound -Builder.setdefaults (notification.default_lights);//Setting the LED inBuilder.setdefaults (notification.default_vibrate);//Set Vibration the //builder.setdefaults (notification.default_all);//Set All Effects
//Permissions <uses-permission android:name= "Android.permission.VIBRATE"/> the //Notification Notification = Builder.build ();//Android version 4.1 and above AboutNotification Notification = Builder.getnotification ();//Android version 4.1 or less the manger.notify (notification_id,notification); the } the}
Pendingintent.getactivity (this, 0, intent, 0);
Parameters:
The second one:
Id
Fourth one:
Set flag bit
Flag_auto_cancel This notification can be cleared by the purge button of the status bar
flag_no_clear This notification can be cleared by the purge button of the status bar
flag_ongoing_event Notifications are placed in the running
Whether the flag_insistent is always on, such as music always playing, know user response
1 // Click the Clear button to clear the message notification, but click on the notification bar notification will not disappear 2 // Clicking the Clear button does not clear the message notification, which can be used to indicate that It will disappear automatically after running 3// Click the Clear button or click the notification 4 // always on, such as music always playing, know user response
:
Android Development _ Talk about notification (notification bar)