Android development using Notification. Builder and androidnotification
The main function of notification is the prompt function. For example, text messages and push messages.
General steps:
1. Retrieve status notification bar management
Icationicationmanager is a system Service, so it must passGetSystemService (icationication_service) method.
notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
2. instantiate icationicationcompat. Builder
3. Set icationicationcompat. Builder
4. Set PendingIntent
5. Display
Methods or parameters:
1.PendingIntent
PendingIntent.getBroadcast(context, requestCode, intent, flags)
PendingIntent.getActivities(context, requestCode, intents, flags)
PendingIntent.getService(context, requestCode, intent, flags)
Flags attribute parameters in:
FLAG_ONE_SHOT indicates that the returned PendingIntent can be executed only once and is automatically canceled after execution.
FLAG_NO_CREATE indicates that if the described PendingIntent does not exist, the corresponding PendingIntent is not created, but NULL is returned.
FLAG_CANCEL_CURRENT indicates that the PendingIntent already exists. Cancel the former and create a new PendingIntent.
FLAG_UPDATE_CURRENT indicates the updated PendingIntent.
2. Introduction to notification. flags Parameters
Notification. FLAG_SHOW_LIGHTS // three-color indicator reminder. This identifier must be added when a three-color indicator is used.
Notification. FLAG_ONGOING_EVENT // initiate a running event (active)
Notification. FLAG_INSISTENT // infinite loop of sound and vibration until the user responds (cancel or open)
Notification. FLAG_ONLY_ALERT_ONCE // after the Notification is initiated, the ringtone and vibration are executed only once.
Notification. FLAG_AUTO_CANCEL // The user automatically disappears after clicking the Notification.
Notification. FLAG_NO_CLEAR // Notification is cleared only when all items are cleared. The Notification is not clear (QQ notifications cannot be cleared, but they are used)
Notification. FLAG_FOREGROUND_SERVICE // indicates the running service.
Usage:
After setting properties, Set
Notification notification =builder.build();notification.flags =Notification.FLAG_ONLY_ALERT_ONCE;
3. setVibrate (long [] pattern)
Permission is required to set the vibration.
<uses-permission android:name="android.permission.VIBRATE"/>
4. builder. setOngoing ()
If it is set to true, it indicates that it is an ongoing notification. To put it simply, when it is true, it cannot be slide away.
**************************************** **************************************** *******
To use a custom Notification, you must use RemoteViews.
**************************************** **************************************** *******
Instance used:
Image:
Implementation Code:
MainActivity. java
Public class MainActivity extends Activity {Button button, button2; icationicationmanager icationmanager; public final static String NEWS_LISTEN = "broadcast"; // used for custom Notification, click Event verification String remoteViewsText = "not clicked"; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); icationicationmanager = (notificationManager) this. g EtSystemService (icationication_service); IntentFilter filter = new IntentFilter (); filter. addAction (NEWS_LISTEN); this. registerReceiver (broadcastReceiver, filter);} public void click (View v) {switch (v. getId () {case R. id. but: // use a normal NotificationNotification. builder builder = new Notification. builder (MainActivity. this); Intent intent = new Intent (MainActivity. this, SecondActivity. class); PendingIntent pen DingIntent = PendingIntent. getActivity (MainActivity. this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT); builder. setContentIntent (pendingIntent); builder. setSmallIcon (R. drawable. close); // set the icon builder. setWhen (System. currentTimeMillis (); // set the time when the notification comes. // builder. setAutoCancel (true); builder. setContentTitle ("title"); // set the notification title builder. setContentText ("content"); // set the notification content builder. setTicker ("display on the status bar"); // builder is displayed on the status bar. SetOngoing (true);/** // set the sound (audio file in the mobile phone) String path = * Environment. getExternalStorageDirectory (). getAbsolutePath () + * "/Music/a.mp3"; File file = new File (path); * builder. setSound (Uri. fromFile (file); * // get the ringtone builder in the Android multi-media library. setSound (Uri. withAppendedPath (Audio. media. INTERNAL_CONTENT_URI, "5"); // builder. setVibrate (new long [] {4000,}); // a real machine test Notification = builder is required. Build (); // notification. flags = Notification. FLAG_ONGOING_EVENT; icationicationmanager. running y (0, notification); break; case R. id. but2: // ButtonMyNotification (); break; case R. id. but3: // use the downloaded Notification. final Notification can be used after 4.0. builder builder3 = new Notification. builder (MainActivity. this); builder3.setSmallIcon (R. drawable. ic_launcher ). setTicker ("showProgressBar "). setConte NtInfo ("contentInfo "). setOngoing (true ). setContentTitle ("ContentTitle "). setContentText ("ContentText"); // simulate the download process new Thread (new Runnable () {@ Overridepublic void run () {int progress = 0; for (progress = 0; progress <100; progress + = 5) {// set the third parameter of setProgress to true to display the progress bar style builder3.setProgress (100, progress, false) without clear progress ); icationicationmanager. Y (0, builder3.build (); try {Thread. sleep (1*1000);} cat Ch (InterruptedException e) {System. out. println ("sleep failure") ;}} builder3.setContentTitle ("Download complete "). setProgress (0, 0, false ). setOngoing (false); icationicationmanager. Y (0, builder3.build ());}}). start (); break; case R. id. but4: // large layout notification can be used after 4.1, BigTextStyleNotification. bigTextStyle textStyle = new Notification. bigTextStyle (); textStyle. setBigContentTitle ("Big title") // Title. setSummaryText ("Summa RyText "). bigText (" Big Text !!!!!!!!!!!!!!!!!!!!!!!!!!!! "+ "!!!!!!!!!!! "+ "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! "); // Content Notification. builder builder2 = new Notification. builder (MainActivity. this); builder2.setSmallIcon (R. drawable. icon); // small icon // large icon builder2.setLargeIcon (BitmapFactory. decodeResource (this. getResources (), R. drawable. close); builder2.setTicker ("showBigView_Text "). setContentInfo ("contentInfo"); builder2.setStyle (textStyle); builder2.setAutoCancel (true); icationicationmanager. notify (0, builder2.build (); break; case R. id. but5: // large layout Notification can be used after 4.1. Large layout picture Notification. bigPictureStyle bigPictureStyle = new Notification. bigPictureStyle (); bigPictureStyle. bigPicture (BitmapFactory. decodeResource (getResources (), R. drawable. back); Notification. builder builder4 = new Notification. builder (MainActivity. this); builder4.setSmallIcon (R. drawable. icon); // small icon // large icon builder4.setLargeIcon (BitmapFactory. decodeResource (this. getResources (), R. drawable. close); builder4.setTicker ("showBigView_Picture "). setContentInfo ("contentInfo"); builder4.setStyle (bigPictureStyle); builder4.setAutoCancel (true); icationicationmanager. using Y (0, builder4.build (); break; case R. id. but6: // large layout notification can be used after 4.1, InboxStyleNotification. inboxStyle inboxStyle = new Notification. inboxStyle (); inboxStyle. setBigContentTitle ("InboxStyle"); inboxStyle. setSummaryText ("Test"); for (int I = 0; I <5; I ++) {inboxStyle. addLine ("new:" + I);} Notification. builder builder5 = new Notification. builder (MainActivity. this); builder5.setSmallIcon (R. drawable. icon); // small icon // large icon builder5.setLargeIcon (BitmapFactory. decodeResource (this. getResources (), R. drawable. close); builder5.setTicker ("showBigView_InboxStyle "). setContentInfo ("contentInfo"); builder5.setStyle (inboxStyle); builder5.setAutoCancel (true); icationicationmanager. Y (0, builder5.build (); break ;}@overrideprotected void onDestroy () {super. onDestroy (); // cancel broadcasting to receive this. unregisterReceiver (broadcastReceiver);}/*** custom Notification */public void MyNotification () {RemoteViews remoteViews = new RemoteViews (getPackageName (), R. layout. form); remoteViews. setTextViewText (R. id. TV _form, remoteViewsText); Intent intent2 = new Intent (MainActivity. NEWS_LISTEN); // use broadcast, so INTENT must use the getBroadcast method PendingIntent pendingIntent2 = PendingIntent. getBroadcast (MainActivity. this, 1, intent2, PendingIntent. FLAG_UPDATE_CURRENT); // bind remoteViews. setOnClickPendingIntent (R. id. but_form, pendingIntent2); Notification. builder builderMain = new Notification. builder (MainActivity. this); builderMain. setContent (remoteViews ). setSmallIcon (R. drawable. icon ). setLargeIcon (BitmapFactory. decodeResource (this. getResources (), R. drawable. open )). setOngoing (true ). setTicker ("music is playing"); icationicationmanager. policy (0, builderMain. build ();} // broadcast receiver (used for custom Notification) BroadcastReceiver broadcastReceiver = new BroadcastReceiver () {@ Overridepublic void onReceive (Context c, Intent intent) {if (intent. getAction (). equals (NEWS_LISTEN) {remoteViewsText = "clicked"; MyNotification ();}}};}
Activity_main.xml
<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:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <Button android:id="@+id/but" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="notification one" /> <Button android:id="@+id/but2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="notification two" /> <Button android:id="@+id/but3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="notification three" /> <Button android:id="@+id/but4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="notification four" /> <Button android:id="@+id/but5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="notification five" /> <Button android:id="@+id/but6" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="notification six" /></LinearLayout>
Form. xml (custom notification style)
<? 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"> <Button android: id = "@ + id/but_form" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <TextView android: id = "@ + id/TV _form" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>
SecondActivity. java is just an activity.