通知栏notification的使用
其中在android的文档中有关于这方面的介绍 具体为 Dev Guide 下的 User iNTERFACE 下的Notifications
其中主要分3种:Toast Notification (Toast就不用说了 大家都知道) 然后是Status Bar Notification(通知栏消息)以及Dialog Notification 这里主要介绍的通知栏
一共4个步骤 在文档中都有说明 下面就直接上代码了 具体的自己看文档吧
代码包含直接发送一个通知,以及自定义notification
两个布局文件
mian.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" /> 11 12 <Button 13 android:id="@+id/button1" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:onClick="click" 17 android:text="显示一个提示消息" /> 18 <Button 19 android:id="@+id/button1" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:onClick="click2" 23 android:text="显示一个自定义的消息" /> 24 </LinearLayout>
notification.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layout" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:padding="10dp" > 7 8 <ImageView 9 android:id="@+id/image" 10 android:layout_width="wrap_content" 11 android:layout_height="fill_parent" 12 android:layout_alignParentLeft="true" 13 android:layout_marginRight="10dp" 14 android:src="@drawable/ic_launcher" /> 15 16 <TextView 17 android:id="@+id/title" 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:layout_toRightOf="@id/image" 21 android:text="我是自定义的标题" /> 22 23 <ProgressBar 24 android:id="@+id/pb" 25 26 android:layout_width="200dip" 27 android:layout_height="wrap_content" 28 android:layout_below="@id/title" 29 android:layout_toRightOf="@id/image" 30 android:progress="30" 31 android:max="100" /> 32 33 34 </RelativeLayout>
activity
1 package cn.itcast.notify; 2 3 import android.app.Activity; 4 import android.app.Notification; 5 import android.app.NotificationManager; 6 import android.app.PendingIntent; 7 import android.content.Intent; 8 import android.os.Bundle; 9 import android.view.View; 10 import android.widget.RemoteViews; 11 12 public class DemoActivity extends Activity { 13 /** Called when the activity is first created. */ 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.main); 18 } 19 public void click(View view){ 20 //1.得到系统与notification相关的服务 21 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 22 23 //2.初始化 一个 notification的对象 ,让notificationmanager显示 24 Notification notification = new Notification(R.drawable.notification, "我是notification", System.currentTimeMillis()); 25 26 //3.设置notification的具体的参数 27 notification.flags = Notification.FLAG_AUTO_CANCEL; //标示当前的notification不会被清除掉 28 // notification.sound 指定notification的声音 29 // notification.vibrate long[]{200,100,200} 30 notification.icon = R.drawable.notification; 31 Intent intent = new Intent(this,DemoActivity.class); 32 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 33 notification.setLatestEventInfo(this, "我是正文的标题", "我是具体的内容", contentIntent); 34 35 //4.利用notification的管理器把notification显示出来 36 nm.notify(0, notification); 37 //nm.cancel(id) 38 } 39 40 public void click2(View view){ 41 //1.得到系统与notification相关的服务 42 NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 43 44 //2.初始化 一个 notification的对象 ,让notificationmanager显示 45 Notification notification = new Notification(R.drawable.notification, "我是notification", System.currentTimeMillis()); 46 47 //3.创建一个remoteview的对象 48 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification); 49 contentView.setTextViewText(R.id.title, "Custom notification"); 50 //contentView.setTextViewText(R.id.text, "This is a custom layout"); 51 contentView.setProgressBar(R.id.pb, 100, 30, true); 52 notification.contentView = contentView; 53 Intent notificationIntent = new Intent(this, DemoActivity.class); 54 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 55 notification.contentIntent = contentIntent; 56 57 //4.利用notification的管理器把notification显示出来 58 nm.notify(2, notification); 59 //nm.cancel(id) 60 } 61 }
基础/day07/notification

浙公网安备 33010602011771号