在android開發中經常要使用到通知,比如:收到短息,來電等等,通知是應用程序提醒用戶的一種方式,他不需要使用Activity。
通知向用戶傳遞信息有多種方式:(1)狀態欄圖標 (2)擴展的通知狀態繪製器 (3)聲音、震動、LED閃爍
通過一個小例子將上面幾種方式集成到一起。
在配置擴展狀態通知顯示的時候,有兩種方法:
1,使用setLatestEventInfo方法更新標準的擴展的狀態通知顯示中所顯示的詳細信息。
2,使用遠程視圖(Remote View)設置contentView和contentIntent,這樣可以爲擴展狀態顯示分配一個你需要的定製UI.
擴展狀態窗口定製佈局 my_status.xml(一個簡單線性佈局,前面放一個imageview,後面一個textview)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/status_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/status_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
要把這個定製佈局分配給通知,要創建一個新的RemoteView對象,並把它分配給contentView屬性,還需要想contentIntent屬性分配一個待處理的意圖(Pending Intent)代碼如下:mNotification.contentView = new RemoteViews(this.getPackageName(),R.layout.my_status); mNotification.contentIntent = mContentIntent;
如果要修改定製佈局中視圖的屬性或者外觀,可以使用遠程視圖對象的set*方法
mNotification.contentView.setImageViewResource(R.id.status_icon,R.drawable.icon);
mNotification.contentView.setTextViewText(R.id.status_text, "This is test content");
向通知添加聲音、閃屏、振動效果的最簡單最一致的方式是使用當前用戶的默認設置。mNotification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
如果想全部默認設置就使用DEFAULT_ALL常量
如果要使用自己的定製的聲音或者動態修改聲音就可以設置mNotification.sound = ringURI;
要設置振動類型的話,需要向通知的vibrate屬性分配一個longs類型的數組;
比如:
long[] vibreate= new long[]{1000,1000,1000,1000,1000}; mNotification.vibrate = vibreate;
上面代碼的作用是振動按照振動1秒,暫停1秒的模式進行振動,整個過程持續5秒。
需要注意的是,使用振動必須添加一個權限:
<uses-permission android:name="android.permission.VIBRATE"/>
設置閃屏
mNotification.ledARGB = Color.BLUE;
mNotification.ledOffMS= 0;
mNotification.ledOnMS = 1;
mNotification.flags = mNotification.flags | Notification.FLAG_SHOW_LIGHTS;
ledARGB屬性可以用來設置LED的顏色,ledOffMS和ledOnMS屬性則可以設置LED閃爍的頻率和模式。ledOnMS設置爲1並把ledOffMS設置爲0來打開LED,兩個都設置爲0則關閉LED.完整的代碼:
主界面佈局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="通知測試小程序" /> <Button android:id="@+id/showStatusButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="測試通知" /> <Button android:id="@+id/cancelButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="取消通知" /> </LinearLayout>
activity
import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews; public class NotificationDemo extends Activity implements OnClickListener{ private Context mContext; private Button showStatusButton,cancelButton; private Notification mNotification; private NotificationManager mNotificationManager; private final static int NOTIFICATION_ID = 0x0001; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mNotification = new Notification(R.drawable.icon,"This is a notification.",System.currentTimeMillis()); mNotificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); findViews(); } public void findViews(){ mContext = NotificationDemo.this; showStatusButton = (Button)findViewById(R.id.showStatusButton); cancelButton = (Button)findViewById(R.id.cancelButton); showStatusButton.setOnClickListener(this); cancelButton.setOnClickListener(this); } public void statusNotifi(){ Intent mIntent = new Intent(mContext,NotificationDemo.class); //這裏需要設置Intent.FLAG_ACTIVITY_NEW_TASK屬性 mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent mContentIntent =PendingIntent.getActivity(mContext,0, mIntent, 0); //1,使用setLatestEventInfo //這裏必需要用setLatestEventInfo(上下文,標題,內容,PendingIntent)不然會報錯. // mNotification.setLatestEventInfo(mContext, "新消息", "主人,您孫子給你來短息了", mContentIntent); //2,使用遠程視圖 mNotification.contentView = new RemoteViews(this.getPackageName(),R.layout.my_status); mNotification.contentView.setImageViewResource(R.id.status_icon,R.drawable.icon); mNotification.contentView.setTextViewText(R.id.status_text, "This is test content"); //使用默認的聲音,閃屏,振動效果 // mNotification.defaults = Notification.DEFAULT_ALL; // mNotification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; //添加震動 long[] vibreate= new long[]{1000,1000,1000,1000}; mNotification.vibrate = vibreate; //添加led mNotification.ledARGB = Color.BLUE; mNotification.ledOffMS= 0; mNotification.ledOnMS = 1; mNotification.flags = mNotification.flags | Notification.FLAG_SHOW_LIGHTS; //手動設置contentView屬於時,必須同時也設置contentIntent不然會報錯 mNotification.contentIntent = mContentIntent; //觸發通知(消息ID,通知對象) mNotificationManager.notify(NOTIFICATION_ID, mNotification); } public void onClick(View v) { if(v == showStatusButton){ statusNotifi(); }else if(v == cancelButton){ mNotificationManager.cancel(NOTIFICATION_ID); //取消通知 } } }
提示:每個設備對LED的控制方面可能具有不同的限制,如果設置的顏色不可用,可以嘗試換用其他顏色試試。
振動和LED在模擬器中是看不到效果的,必須使用真機。
Android通知管理(NotificationManager)的使用,包括震動,led閃屏
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.