The Notification in the top bar of the Android homepage can customize the style of the Notification message bar and click the Notification column to enter the people-oriented program ., Android custom message bar

Source: Internet
Author: User

The Notification in the top bar of the Android homepage can customize the style of the Notification message bar and click the Notification column to enter the people-oriented program ., Android custom message bar

Common Program notifications are displayed in the top bar of the home page.

1 package com. lixu. tongzhi; 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. intent; 8 import android. OS. bundle; 9 import android. support. v4.app. notificationCompat; 10 import android. view. view; 11 import android. view. view. onClickListener; 12 import android. widget. button; 13 import Ndroid. widget. remoteViews; 14 import android. widget. toast; 15 16 public class MainActivity extends Activity {17 18 private static final int ID = 1987; 19 private static final int REQUEST_CODE = 123; 20 21 @ Override22 protected void onCreate (Bundle savedInstanceState) {23 super. onCreate (savedInstanceState); 24 setContentView (R. layout. activity_main); 25 26 Button btn1 = (Button) findViewById (R. id. fasong ); 27 Button btn2 = (Button) findViewById (R. id. qingchu); 28 29 btn1.setOnClickListener (new OnClickListener () {30 31 @ Override32 public void onClick (View v) {33 send (); 34 35 Toast. makeText (getApplicationContext (), "notification sent successfully! ", 0 ). show (); 36 37} 38}); 39 40 btn2.setOnClickListener (new OnClickListener () {41 42 @ Override43 public void onClick (View v) {44 45 delete (); 46 47 Toast. makeText (getApplicationContext (), "Notification cleared successfully! ", 0 ). show (); 48 49} 50}); 51 52} 53 54 private void send () {55 // get notification manager 56 icationicationmanager manager = (icationicationmanager) getSystemService (icationication_service ); 57 58 icationicationcompat. builder mBuilder = new NotificationCompat. builder (this); 59 // set the notification bar image 60 mBuilder. setSmallIcon (R. drawable. sdfdf); 61 62 Notification notification = mBuilder. build (); 63 // RemoteViews custom Notification layout 64 65 Remot EViews cv = new RemoteViews (getApplicationContext (). getPackageName (), R. layout. list); 66 // set the picture 67 cv in the notification bar after the drop-down. setImageViewResource (R. id. image, R. drawable. meimei); 68 // sets the content of 69 cv. setTextViewText (R. id. tv1, "I'm a Superman"); 70 cv. setTextViewText (R. id. tv2, "I'm a Superman, and the master has something to tell me. "); 71 notification. contentView = cv; 72 73 // set the notification sound or vibrate or flash. 74 notification. defaults = Notification. DEFAULT_SOUND | Notification. DEFAULT_VIBRATE; 75 // notification time 76 notification. when = System. currentTimeMillis (); 77 78 // as an option, you can set the MainActivity startup mode to singleTop to avoid repeated onCreate (). 79 Intent intent = new Intent (getApplicationContext (), MainActivity. class); 80 // when the user clicks Notification in the Notification bar, switch back to MainActivity. 81 PendingIntent pi = PendingIntent. getActivity (getApplicationContext (), REQUEST_CODE, intent, 82 PendingIntent. FLAG_UPDATE_CURRENT); 83 notification. contentIntent = pi; 84 85 // send this notification with a specific id 86 manager. notify (ID, notification); 87 88} 89 90 private void delete () {91 // get notification manager 92 icationicationmanager manager = (icationicationmanager) getSystemService (icationication_service); 93 manager. cancel (ID); 94 95} 96 97}

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.

Xml file:

1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent"> 5 6 <Button 7 android: id = "@ + id/fasong" 8 android: layout_width = "wrap_content" 9 android: layout_height = "wrap_content" 10 android: layout_alignLeft = "@ + id/qingchu" 11 android: layout_alignParentTop = "true" 12 android: layout_marginTop = "56dp" 13 android: text = "send notification"/> 14 15 <Button16 android: id = "@ + id/qingchu" 17 android: layout_width = "wrap_content" 18 android: layout_height = "wrap_content" 19 android: layout_below = "@ + id/fasong" 20 android: layout_centerHorizontal = "true" 21 android: layout_marginTop = "88dp" 22 android: text = "clear notification"/> 23 24 </RelativeLayout>
 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/image" 8         android:layout_width="50dp" 9         android:layout_height="50dp"10         android:layout_alignParentLeft="true"11         android:layout_marginRight="10dp" />12 13     <TextView14         android:id="@+id/tv1"15         android:layout_width="wrap_content"16         android:layout_height="wrap_content"17         android:layout_toRightOf="@id/image"18         android:background="#ff0000" />19 20     <TextView21         android:id="@+id/tv2"22         android:layout_width="wrap_content"23         android:layout_height="wrap_content"24         android:layout_below="@id/tv1"25         android:layout_toRightOf="@id/image"26         android:background="#00ff00" />27 28 </RelativeLayout>


Run:

 

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.