一、BroadcastReceiver介紹
BroadcastReceiver是Android的四大元件之一,與Activity和Service具有完整聲明周期不同。它本質上是一種全局監聽器,用于監聽系統全局的廣播消息。是以它可以友善地實作系統中不同元件之間的通信。
二、BroadcastReceiver的使用
BroadcastReceiver用于接收程式所發出的 Broadcast Intent事件,它的啟動使用步驟:
1、建立class繼承BroadcastReceiver。
2、注冊廣播(靜态注冊于動态注冊)。
2、建立Intent(這裡Intent的啟動可以是隐式啟動也可以是顯示啟動)。
3、new BroadcastReceiver()對象調用sendBroadcast(intent)方法。
三、簡單執行個體使用:
1、建立MyReceiver 類 繼承BroadcastReceiver(重寫onReceive方法)
public class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// Toast.makeText(context, "我接收到了廣播",Toast.LENGTH_LONG ).show();
Log.d("廣播", "我接收到了廣播");
}
}
注意:
1、如果系統找不到Activity的元件可能會報異常,程式終止,但是BroadcastReceiver如果找不到合适的元件應用不會有任何異常。
2、onReceive方法不中不能有耗時操作,如果onReceive方法在10秒内不能執行完成則認為該程序無響應。
2、靜态注冊與動态注冊
♬(1)靜态注冊(
<receiver android:name="com.example.mybroadcast.MyReceiver" > </receiver>
)
下面添加在manifest中的注冊(包含了一個隐式啟動)
<receiver android:name="com.example.mybroadcast.MyReceiver"
<intent-filter>
<action android:name="hahaha"
</intent-filter>
</receiver>
♬(2)動态注冊(
registerReceiver(mreceiver, filter);
)
在Activity中進行動态注冊廣播的時候需要覆寫onDestroy方法用于登出。
//代碼中注冊廣播相當于manifest中的注冊
mreceiver=new MyReceiver();
IntentFilter filter=new IntentFilter();
filter.addAction("hahaha");
registerReceiver(mreceiver, filter);
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
unregisterReceiver(mreceiver);
}
4、MainActivity中設定(建立類、建立Intent、發送廣播)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_send:
Intent intent=new Intent();
intent.setAction("hahaha");
sendBroadcast(intent);
// Toast.makeText(getApplicationContext(), "發送廣播",Toast.LENGTH_LONG ).show();
break;
default:
break;
}
}
四、鬧鐘廣播
mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
private void cancelalarm() {
Intent intent = new Intent();
intent.setAction("hahaha");
PendingIntent pending = PendingIntent.getBroadcast(
getApplicationContext(), 0x22, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.cancel(pending);
}
private void wakeupalarm() {
Intent intent = new Intent();
intent.setAction("hahaha");
PendingIntent pending = PendingIntent.getBroadcast(
getApplicationContext(), 0x22, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + 5000, 5000, pending);
}
五、接收系統廣播消息
1、Android 系統廣播大全
2、執行個體:監聽app解除安裝與wifi狀态
添加
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.broadcast_packaged_removed"/>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED"
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
完整
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcast"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.broadcast_packaged_removed"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
<activity
android:name="com.example.mybroadcast.MainActivity"
android:label="@string/app_name"
<intent-filter>
<action android:name="android.intent.action.MAIN"
<category android:name="android.intent.category.LAUNCHER"
</intent-filter>
</activity>
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED"
<action android:name="android.net.wifi.STATE_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>