Notification (2) notification usage that you may not know
Here is one of the notifications, Notification. Common and custom
Notificaton does not interrupt the operation of the Activity.
In the status bar or notification form, there can be some warning settings, such as playing sound, flashlight
Images and title messages should be included (custom notifications are not needed), and a PendingIntent should be used to tell the notification source (Activity, Broadcase, Service)
Optional text description, warning sound, vibration, LED flashlight
<RelativeLayout 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 = "com. example. statusnotification. mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" fill_parent "android: layout_height =" wrap_content "android: layout_alignParentTop =" true "android: layout_centerHorizontal = "true" android: layout_marginTop = "65dp" android: text = "usage of common notifications"/> <Button android: id = "@ + id/button2" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" android: text = "use of custom notifications"/> </RelativeLayout>
Customize the layout used by notifications
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" android:layout_marginLeft="30dp" android:src="@drawable/notify" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/imageView1" android:text="TextView1" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/imageView1" android:layout_below="@id/textView1" android:text="TextView2" /></RelativeLayout>
Package com. example. statusnotification; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. intent; import android.net. uri; 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 {private Button button1; private icationicationmanager NotificationManager; // Notification management class private Notification. builder builder; private Button button2; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button1 = (Button) findViewById (R. id. button1); button2 = (Button) findViewById (R. id. button2); // use getSystemService to instantiate the Notification icationicationmanager = (icationicationmanager) getSystemService (NOTIFICATION_SERVICE); builder = new Notification. builder (MainActivity. this); button1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubIntent intent = new Intent (MainActivity. this, MainActivity. class); PendingIntent pendingIntent = PendingIntent. getActivity (MainActivity. this, 0, intent, 0); builder. setContentIntent (pendingIntent); builder. setContentTitle ("Notification prompt"); builder. setContentText ("tomorrow's holiday, coming off work early today"); builder. setSmallIcon (R. drawable. notify); builder. setTicker ("new notification"); // The builder is visible when the first notification is sent. setDefaults (Notification. DEFAULT_ALL ); /* DEFAULT_ALL default * DEFAULT_LIGHTS default flash * DEFAULT_SOUND default ringtone * DEFAULT_VIBRATE default vibration * you can set the default flashlight, sound, and vibration by specifying different parameters of Notification * not in AndroidManifest. xml forgot to authorize **/Uri uri = Uri. parse ("file: // mnt/sdcard/Music/audioaudio"); builder. setSound (uri); Notification notification = builder. build (); // only available for Android4.1 and later versions, and new Notification for earlier versions. // use notification for earlier versions. defaults = Notification. DEFAULT_SOUND/** first parameter: identifier id * second parameter: Notification * // long [] vibrates = {10, 20, 30}; // notification. vibrate = vibrates; // uses an array to set the vibration time icationicationmanager. listen y (88, notification) ;}}); button2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// you must use RemoteViewsRemoteViews view = new RemoteViews (getPackageName (), R. layout. custom_notification); view. setImageViewResource (R. id. imageView1, R. drawable. optional Y); view. setTextViewText (R. id. textView1, "notification"); view. setTextViewText (R. id. textView2, "dinner together tonight"); Intent intent = new Intent (MainActivity. this, MainActivity. class); PendingIntent pendingIntent = PendingIntent. getActivity (MainActivity. this, 0, intent, 0); builder. setContentIntent (pendingIntent); builder. setContent (view); // do not forget to specify its layout Notification = builder. build (); icationicationmanager. Y (89, notification );}});}}You can set custom notification ringtones for projects. mp3 files are placed in SDCard. The master node can have all different sdcarddirectories under the mntdirectory. The MP3 file here is audioaudio.
Running result
Source code download
Click the common notification button and then the custom notification button. The result is as expected. However, if you click the custom notification button and then click the common notification button, all the notifications are displayed in the Custom notification layout and content .?? Is it a thread problem ??
When eclipse is found to be very strange today, an "Android4.1" "new Notification" in the comment can also report an error to me, so be sure to clean it.