Send Message Notification in the notification bar (you can use a custom layout) to customize Notification

Source: Internet
Author: User

Send Message Notification in the notification bar (you can use a custom layout) to customize Notification

A Simple Application Scenario: if the user opens the Activity and presses the Home key, the Activity enters-> onPause ()-> onStop () invisible. The code sends a Notification to the Notification bar at this time. After you click Notification in the Notification bar, you can switch onRestart ()-> onStart ()-> onResume () back to the original Activity.

1 package com. zzw. testnotification; 2 3 import android. app. activity; 4 import android. app. notification; 5 import android. app. icationicationmanager; 6 import android. app. pendingIntent; 7 import android. content. context; 8 import android. content. intent; 9 import android. OS. bundle; 10 import android. support. v4.app. icationicationcompat. builder; 11 import android. util. log; 12 import android. widget. remoteView S; 13 14 public class MainActivity extends Activity {15 16 private static final String TAG = "---->"; 17 18 private final int NOTIFICATION_ID = 0xa01; 19 private final int REQUEST_CODE = 0xb01; 20 21 @ Override 22 protected void onCreate (Bundle savedInstanceState) {23 super. onCreate (savedInstanceState); 24 setContentView (R. layout. activity_main); 25 Log. d (TAG, "onCreate"); 26} 27 28 @ Override 29 Protected void onResume () {30 Log. d (TAG, "onResume"); 31 super. onResume (); 32} 33 34 @ Override 35 protected void onDestroy () {36 Log. d (TAG, "onDestroy"); 37 super. onDestroy (); 38} 39 40 @ Override 41 protected void onPause () {42 Log. d (TAG, "onPause"); 43 super. onPause (); 44} 45 46 @ Override 47 protected void onRestart () {48 Log. d (TAG, "onRestart"); 49 super. onRestart (); 50} 51 52 @ Override 53 protected void onStart () {54 Log. d (TAG, "onStart"); 55 super. onStart (); 56} 57 58 @ Override 59 protected void onStop () {60 super. onStop (); 61 Log. d (TAG, "onStop"); 62 sendNotification (this, icationication_id, "this is the title", "this is the content "); 63} 64 65 66 // use 67 private void sendNotification (Context context, int icationication_id, String title, String content) {68 icationicationmanager notificationM as a message template in the send notification bar Anager = (icationicationmanager) getSystemService (NOTIFICATION_SERVICE); 69 70 // use the default notification bar layout 71 Builder builder = new Builder (context ); 72 // The icon set here is only used to display the new notification in the notification Bar 73 builder. setSmallIcon (R. drawable. ic_launcher); 74 builder. setContentTitle (title); 75 builder. setContentText (content); 76 77 Notification notification = builder. build (); 78 79/* use custom notification bar layout 80 * when the user goes down the notification bar, the custom Notifi is displayed in RemoteViews. Cation layout 81 */82 // RemoteViews contentView = new RemoteViews (context. getPackageName (), 83 // R. layout. notification); 84 // contentView. setImageViewResource (R. id. imageView, R. drawable. ic_launcher); 85 // contentView. setTextViewText (R. id. title, "Turkish and IS secrets"); 86 // contentView. setTextViewText (R. id. text, "Turkey refuses to apologize to Russia, suspect there IS a supported waist"); 87 // notification. contentView = contentView; 88 89 // when a notification is sent to the notification bar: Sound + The Mobile Phone Vibrates + lights up the Android phone breathing light. 90 // note !! (Sound prompt + mobile phone shake) These two items are basically supported by Android phones. 91 // but whether Android breathing lights can be lit depends on the settings of various mobile phone hardware manufacturers. 92 notification. defaults = Notification. DEFAULT_SOUND | Notification. DEFAULT_VIBRATE | Notification. DEFAULT_LIGHTS; 93 94 // click notification to automatically disappear 95 notification. flags = Notification. FLAG_AUTO_CANCEL; 96 97 // notification time 98 notification. when = System. currentTimeMillis (); 99 100 // as an option, you can set the MainActivity startup mode to singleTop to avoid repeated onCreate (). 101 Intent intent = new Intent (context, MainActivity. class); 102 103 // when the user clicks Notification in the Notification bar, switch back to MainActivity. 104 PendingIntent pi = PendingIntent. getActivity (context, REQUEST_CODE, intent, PendingIntent. flag_cancel_present); 105 notification. contentIntent = pi; 106 107 // notification bar sent to mobile phone 108 icationicationmanager. Y (ication_id _id, notification); 109} 110 111 // use 112 private void deleteNotification (int id) {113 NotificationManager icationicationmanager = (icationicationmanager) as a message template for clearing the notification bar) getSystemService (icationication_service); 114 icationicationmanager. cancel (id); 115} 116}

It should be noted that the Android Activity is the standard mode by default, that is, every time a new Activity is generated, it is not the original Activity. In this example, we can see that if onCreate () in MainActivity does not modify the start mode, the time displayed for each TextView call is different (incrementing ), to use the original Activity and avoid repeating the new one, you must:

In AndroidManifest. xml, modify the MainActivity startup mode to singleTop.

<activity            android:name=".MainActivity"            android:label="@string/app_name"            android:launchMode="singleTop" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

Source code of the notification. xml file:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 6 <ImageView 7 android: id = "@ + id/imageView" 8 android: layout_width = "50dp" 9 android: layout_height = "50dp" 10 android: layout_alignParentLeft = "true" 11 android: layout_centerVertical = "true" 12 android: src = "@ drawable/ic_launcher"/> 13 14 <TextView15 android: id = "@ + id/title" 16 android: layout_width = "wrap_content" 17 android: layout_height = "wrap_content" 18 android: layout_above = "@ + id/text" 19 android: layout_alignParentRight = "true" 20 android: layout_alignTop = "@ + id/imageView" 21 android: layout_marginLeft = "18dp" 22 android: layout_toRightOf = "@ + id/imageView" 23 android: gravity = "center_vertical" 24 android: singleLine = "true" 25 android: text = "TextView"/> 26 27 <TextView28 android: id = "@ + id/text" 29 android: layout_width = "wrap_content" 30 android: layout_height = "wrap_content" 31 android: layout_alignBottom = "@ + id/imageView" 32 android: layout_alignLeft = "@ + id/title" 33 android: gravity = "center_vertical" 34 android: singleLine = "true" 35 android: text = "TextView"/> 36 37 38 </RelativeLayout>Notification. xml

Due to different sdk versions, some users need to add the vibration permission:

<uses-permission android:name="android.permission.VIBRATE"/>

 

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.