通知欄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號