1)Broadcast Receive是系統(tǒng)級(jí)別的事件處理機(jī)制。
2)自定義一個(gè)廣播步驟: 在程序組件中構(gòu)建要廣播的Intent,使用sendBroadcast方法發(fā)送出去; 定義一個(gè)廣播接收器(該接收器繼承BroadcastReceiver),覆蓋onReceive方法響應(yīng)事件。最后注冊(cè)
該廣播接收器(可在代碼中注冊(cè),也可在AndroidManifest.xml配置文件中注冊(cè))。
3)在AndroidManifest.xml配置文件中注冊(cè)廣播接收器:
<receiver android:name="MyReceiver"> <intent-filter> <action android:name="com.amaker.ch08.action.MY_ACTION"/> </intent-filter> </receiver> |
在代碼中注冊(cè),一般在Activity.onResume()方法中使用Context.registerReceiver()方法注冊(cè)一個(gè)廣播
接收器,在Activity.onPause()中使用Contenxt.unregisterReceiver()方法來注銷一個(gè)廣播接收器。
注冊(cè): IntentFilter filter = new IntentFilter(); MyReceiver r = new Myreceiver(); registerReceiver(r,filter); 注銷: unregisterReceiver(r); |
4)BroadcastReceiver組件并沒提供可視化的界面來顯示廣播信息,需要使用Notification和
NotificationManager來實(shí)現(xiàn)可視化的信息顯示。通過它們可以顯示廣播信息的內(nèi)容、圖標(biāo)以及振動(dòng)等信
息。其使用步驟一般先獲得系統(tǒng)級(jí)的服務(wù)NotificationManager,然后實(shí)例化Notification,設(shè)置其屬性
,通過NotificationManager發(fā)出通知即可。
String service = NOTIFICATION_SERVICE; NotificationManager nm = (NotificationManager)getSystemService(service);
Notification n = new Notification(); int icon = n.icon = R.drawable.icon;//設(shè)置顯示圖標(biāo),該圖標(biāo)會(huì)在狀態(tài)欄顯示 String tickerText = "Test Notification";//設(shè)置顯示提示信息,該圖標(biāo)也在狀態(tài)欄顯示 long when = System.currnetTImeMillis(); //顯示時(shí)間 n.icon=icon; n.tickerText = tickerText; n.when = when; //為Notification設(shè)置提示音 n.defaults |=Notification.DEFAULT_SOUND; n.sound = Uri.parse("file:///sdcard/sound.mp3"); n.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); //為Notification設(shè)置振動(dòng) n.defaults |= Notification.DEFAULT_VIBRATE; long[] vibrate = {0,50,100,150}; //0ms后震動(dòng),震動(dòng)50ms后停止,再過100ms震動(dòng),震動(dòng)150ms n.vibrate = vibrate;
Intent intent = new Intent(this, MainActivity.class); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);//獲得PendingIntent n.setLatestEventInfo(this, "My Title", "My Content, pi); //設(shè)置事件信息 int ID = 1; //標(biāo)志該通知的ID nm.notify(ID,n); //發(fā)出通知 | 5)AlarmManager提供一種系統(tǒng)級(jí)的提示服務(wù),允許你安排在將來的某個(gè)時(shí)間執(zhí)行一個(gè)服務(wù)。
AlarmManager對(duì)象一般不直接實(shí)例化,而是通過Context.getSystemService(Context.ALARM_SERVICE)方
法獲得。
AlarmManager的使用步驟: 獲得AlarmManager實(shí)例; 定義一個(gè)PendingIntent發(fā)出廣播; 調(diào)用AlarmManager的方法,設(shè)置定時(shí)或重復(fù)提醒。
final AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(); intent.setAction(MY_ACTION); intent.putExtra("msg", "你該起床了!"); final PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0); final long time = System.currentTimeMillis(); |
然后可以調(diào)用AlarmManager的方法進(jìn)行相應(yīng)操作。
AlarmManager常用屬性和方法:
實(shí)際遇到的問題:
在定義廣播接收類的時(shí)候,如果先在所在activity的類中聲明這個(gè)類,再在后面定義,則程序報(bào)錯(cuò),形如:
public class SplashActivity extends Activity { private BroadcastReceiver connectionReceiver;
.......
connectionReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { } };
至于為什么報(bào)錯(cuò)還不清楚。但是網(wǎng)上一般的寫法都是
private BroadcastReceiver connectionReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { } };
|