Android(安卓)BroadCast
1. 自定义自已的Broadcast receiver:
public class myReceiver extends BroadcastReceiver{
@Override
public void onReceive (Context context, Intent intent) {
if (intent.getAction().equals("com.spreadcomm.myparceltest.test")){
Toast.makeText(context, "This is from brodcast!", Toast.LENGTH_SHORT).show();
}
else if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
Toast.makeText(context, "Charing up!", Toast.LENGTH_LONG).show();
}
}
}
2. 注册Receiver:
a. 通过context:
registerReceiver (BroadcastReceiver receiver, IntentFilter filter, String broadcastPermission, Handler scheduler)
b. 通过Manifest.xml:
<activity android:name=".myParceTest" 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="com.myparceltest.test" />
</intent-filter>
</receiver>
3. 发送Broadcast:
4. Anroid 提供的Brodcast Action
ACTION_TIME_TICK | 每分钟发送,表明时间在运行 |
ACTION_TIME_CHANGED | 当用户更改了设备上的时间时发送 |
ACTION_TIMEZONE_CHANGED | 当用更改了设备上的时区时发送 |
ACTION_BOOT_COMPLETED | 当设备启动完成了以后 |
ACTION_PACKAGE_ADDED | 当添加了新的包到设备上时发送 |
ACTION_PACKAGE_REMOVED | 当删除一个包时发送 |
ACTION_PACKAGE_CHANGED | 当包被更改时,如disable 改为 |
ACTION_PACKAGE_RESTARTED | 当程序被中断,重新打开的时候 |
ACTION_UID_REMOVED | 一个UID从系统删除的时候 |
ACTION_SHUTDOWN | 关机的时候 |
ACTION_POWER_DISCONNECTED | 断开连接电源 |
ACTION_POWER_CONNECTED | 连接外部电源时 |
ACTION_BATTERY_CHANGED | 当电池的状态,充电改变时发送 |
ACTION_PACKAGE_DATA_CLEARED | The user has cleared the data of a package. This should be preceded by ACTION_PACKAGE_RESTARTED , after which all of its persistent data is erased and this broadcast sent. Note that the cleared package does not receive this broadcast. |
Api Level 3:(SDK 1.5)
Xml代码
- android.bluetooth.a2dp.intent.action.SINK_STATE_CHANGED
- android.bluetooth.intent.action.BLUETOOTH_STATE_CHANGED
- android.bluetooth.intent.action.BOND_STATE_CHANGED_ACTION
- android.bluetooth.intent.action.DISCOVERY_COMPLETED
- android.bluetooth.intent.action.DISCOVERY_STARTED
- android.bluetooth.intent.action.HEADSET_ADUIO_STATE_CHANGED
- android.bluetooth.intent.action.HEADSET_STATE_CHANGED
- android.bluetooth.intent.action.NAME_CHANGED
- android.bluetooth.intent.action.PAIRING_CANCEL
- android.bluetooth.intent.action.PAIRING_REQUEST
- android.bluetooth.intent.action.REMOTE_DEVICE_CONNECTED
- android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED
- android.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEARED
- android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECTED
- android.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECT_REQUESTED
- android.bluetooth.intent.action.REMOTE_DEVICE_FOUND
- android.bluetooth.intent.action.REMOTE_NAME_FAILED
- android.bluetooth.intent.action.REMOTE_NAME_UPDATED
- android.bluetooth.intent.action.SCAN_MODE_CHANGED
- android.intent.action.AIRPLANE_MODE
- android.intent.action.BATTERY_CHANGED
- android.intent.action.BATTERY_LOW
- android.intent.action.BOOT_COMPLETED
- android.intent.action.CAMERA_BUTTON
- android.intent.action.CONFIGURATION_CHANGED
- android.intent.action.DATA_SMS_RECEIVED
- android.intent.action.DATE_CHANGED
- android.intent.action.DEVICE_STORAGE_LOW
- android.intent.action.DEVICE_STORAGE_OK
- android.intent.action.GTALK_CONNECTED
- android.intent.action.GTALK_DISCONNECTED
- android.intent.action.HEADSET_PLUG
- android.intent.action.INPUT_METHOD_CHANGED
- android.intent.action.MANAGE_PACKAGE_STORAGE
- android.intent.action.MEDIA_BAD_REMOVAL
- android.intent.action.MEDIA_BUTTON
- android.intent.action.MEDIA_CHECKING
- android.intent.action.MEDIA_EJECT
- android.intent.action.MEDIA_MOUNTED
- android.intent.action.MEDIA_NOFS
- android.intent.action.MEDIA_REMOVED
- android.intent.action.MEDIA_SCANNER_FINISHED
- android.intent.action.MEDIA_SCANNER_SCAN_FILE
- android.intent.action.MEDIA_SCANNER_STARTED
- android.intent.action.MEDIA_SHARED
- android.intent.action.MEDIA_UNMOUNTABLE
- android.intent.action.MEDIA_UNMOUNTED
- android.intent.action.NEW_OUTGOING_CALL
- android.intent.action.PACKAGE_ADDED
- android.intent.action.PACKAGE_CHANGED
- android.intent.action.PACKAGE_DATA_CLEARED
- android.intent.action.PACKAGE_INSTALL
- android.intent.action.PACKAGE_REMOVED
- android.intent.action.PACKAGE_REPLACED
- android.intent.action.PACKAGE_RESTARTED
- android.intent.action.PHONE_STATE
- android.intent.action.PROVIDER_CHANGED
- android.intent.action.REBOOT
- android.intent.action.SCREEN_OFF
- android.intent.action.SCREEN_ON
- android.intent.action.TIMEZONE_CHANGED
- android.intent.action.TIME_SET
- android.intent.action.TIME_TICK
- android.intent.action.UID_REMOVED
- android.intent.action.UMS_CONNECTED
- android.intent.action.UMS_DISCONNECTED
- android.intent.action.USER_PRESENT
- android.intent.action.WALLPAPER_CHANGED
- android.media.AUDIO_BECOMING_NOISY
- android.media.RINGER_MODE_CHANGED
- android.media.VIBRATE_SETTING_CHANGED
- android.net.conn.BACKGROUND_DATA_SETTING_CHANGED
- android.net.wifi.NETWORK_IDS_CHANGED
- android.net.wifi.RSSI_CHANGED
- android.net.wifi.SCAN_RESULTS
- android.net.wifi.STATE_CHANGE
- android.net.wifi.WIFI_STATE_CHANGED
- android.net.wifi.supplicant.CONNECTION_CHANGE
- android.net.wifi.supplicant.STATE_CHANGE
- android.provider.Telephony.SIM_FULL
- android.provider.Telephony.SMS_RECEIVED
- android.provider.Telephony.WAP_PUSH_RECEIVED
- com.google.gservices.intent.action.GSERVICES_CHANGED
android.bluetooth.a2dp.intent.action.SINK_STATE_CHANGEDandroid.bluetooth.intent.action.BLUETOOTH_STATE_CHANGEDandroid.bluetooth.intent.action.BOND_STATE_CHANGED_ACTIONandroid.bluetooth.intent.action.DISCOVERY_COMPLETEDandroid.bluetooth.intent.action.DISCOVERY_STARTEDandroid.bluetooth.intent.action.HEADSET_ADUIO_STATE_CHANGEDandroid.bluetooth.intent.action.HEADSET_STATE_CHANGEDandroid.bluetooth.intent.action.NAME_CHANGEDandroid.bluetooth.intent.action.PAIRING_CANCELandroid.bluetooth.intent.action.PAIRING_REQUESTandroid.bluetooth.intent.action.REMOTE_DEVICE_CONNECTEDandroid.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEAREDandroid.bluetooth.intent.action.REMOTE_DEVICE_DISAPPEAREDandroid.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECTEDandroid.bluetooth.intent.action.REMOTE_DEVICE_DISCONNECT_REQUESTEDandroid.bluetooth.intent.action.REMOTE_DEVICE_FOUNDandroid.bluetooth.intent.action.REMOTE_NAME_FAILEDandroid.bluetooth.intent.action.REMOTE_NAME_UPDATEDandroid.bluetooth.intent.action.SCAN_MODE_CHANGEDandroid.intent.action.AIRPLANE_MODEandroid.intent.action.BATTERY_CHANGEDandroid.intent.action.BATTERY_LOWandroid.intent.action.BOOT_COMPLETEDandroid.intent.action.CAMERA_BUTTONandroid.intent.action.CONFIGURATION_CHANGEDandroid.intent.action.DATA_SMS_RECEIVEDandroid.intent.action.DATE_CHANGEDandroid.intent.action.DEVICE_STORAGE_LOWandroid.intent.action.DEVICE_STORAGE_OKandroid.intent.action.GTALK_CONNECTEDandroid.intent.action.GTALK_DISCONNECTEDandroid.intent.action.HEADSET_PLUGandroid.intent.action.INPUT_METHOD_CHANGEDandroid.intent.action.MANAGE_PACKAGE_STORAGEandroid.intent.action.MEDIA_BAD_REMOVALandroid.intent.action.MEDIA_BUTTONandroid.intent.action.MEDIA_CHECKINGandroid.intent.action.MEDIA_EJECTandroid.intent.action.MEDIA_MOUNTEDandroid.intent.action.MEDIA_NOFSandroid.intent.action.MEDIA_REMOVEDandroid.intent.action.MEDIA_SCANNER_FINISHEDandroid.intent.action.MEDIA_SCANNER_SCAN_FILEandroid.intent.action.MEDIA_SCANNER_STARTEDandroid.intent.action.MEDIA_SHAREDandroid.intent.action.MEDIA_UNMOUNTABLEandroid.intent.action.MEDIA_UNMOUNTEDandroid.intent.action.NEW_OUTGOING_CALLandroid.intent.action.PACKAGE_ADDEDandroid.intent.action.PACKAGE_CHANGEDandroid.intent.action.PACKAGE_DATA_CLEAREDandroid.intent.action.PACKAGE_INSTALLandroid.intent.action.PACKAGE_REMOVEDandroid.intent.action.PACKAGE_REPLACEDandroid.intent.action.PACKAGE_RESTARTEDandroid.intent.action.PHONE_STATEandroid.intent.action.PROVIDER_CHANGEDandroid.intent.action.REBOOTandroid.intent.action.SCREEN_OFFandroid.intent.action.SCREEN_ONandroid.intent.action.TIMEZONE_CHANGEDandroid.intent.action.TIME_SETandroid.intent.action.TIME_TICKandroid.intent.action.UID_REMOVEDandroid.intent.action.UMS_CONNECTEDandroid.intent.action.UMS_DISCONNECTEDandroid.intent.action.USER_PRESENTandroid.intent.action.WALLPAPER_CHANGEDandroid.media.AUDIO_BECOMING_NOISYandroid.media.RINGER_MODE_CHANGEDandroid.media.VIBRATE_SETTING_CHANGEDandroid.net.conn.BACKGROUND_DATA_SETTING_CHANGEDandroid.net.wifi.NETWORK_IDS_CHANGEDandroid.net.wifi.RSSI_CHANGEDandroid.net.wifi.SCAN_RESULTSandroid.net.wifi.STATE_CHANGEandroid.net.wifi.WIFI_STATE_CHANGEDandroid.net.wifi.supplicant.CONNECTION_CHANGEandroid.net.wifi.supplicant.STATE_CHANGEandroid.provider.Telephony.SIM_FULLandroid.provider.Telephony.SMS_RECEIVEDandroid.provider.Telephony.WAP_PUSH_RECEIVEDcom.google.gservices.intent.action.GSERVICES_CHANGED
Api Level 4增加的:(SDK 1.6)
Xml代码
- android.intent.action.ACTION_POWER_CONNECTED
- android.intent.action.ACTION_POWER_DISCONNECTED
- android.intent.action.ACTION_SHUTDOWN
- android.intent.action.BATTERY_OKAY
- android.speech.tts.TTS_QUEUE_PROCESSING_COMPLETED
- android.speech.tts.engine.TTS_DATA_INSTALLED
- com.google.gservices.intent.action.GSERVICES_OVERRIDE
android.intent.action.ACTION_POWER_CONNECTEDandroid.intent.action.ACTION_POWER_DISCONNECTEDandroid.intent.action.ACTION_SHUTDOWNandroid.intent.action.BATTERY_OKAYandroid.speech.tts.TTS_QUEUE_PROCESSING_COMPLETEDandroid.speech.tts.engine.TTS_DATA_INSTALLEDcom.google.gservices.intent.action.GSERVICES_OVERRIDE
Api Level 5(SDK 2.0)蓝牙部分全部修改,修改后的
Xml代码
- android.bluetooth.a2dp.action.SINK_STATE_CHANGED
- android.bluetooth.adapter.action.DISCOVERY_FINISHED
- android.bluetooth.adapter.action.DISCOVERY_STARTED
- android.bluetooth.adapter.action.LOCAL_NAME_CHANGED
- android.bluetooth.adapter.action.SCAN_MODE_CHANGED
- android.bluetooth.adapter.action.STATE_CHANGED
- android.bluetooth.device.action.ACL_CONNECTED
- android.bluetooth.device.action.ACL_DISCONNECTED
- android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED
- android.bluetooth.device.action.BOND_STATE_CHANGED
- android.bluetooth.device.action.CLASS_CHANGED
- android.bluetooth.device.action.FOUND
- android.bluetooth.device.action.NAME_CHANGED
- android.bluetooth.devicepicker.action.DEVICE_SELECTED
- android.bluetooth.devicepicker.action.LAUNCH
- android.bluetooth.headset.action.AUDIO_STATE_CHANGED
- android.bluetooth.headset.action.STATE_CHANGED
android.bluetooth.a2dp.action.SINK_STATE_CHANGEDandroid.bluetooth.adapter.action.DISCOVERY_FINISHEDandroid.bluetooth.adapter.action.DISCOVERY_STARTEDandroid.bluetooth.adapter.action.LOCAL_NAME_CHANGEDandroid.bluetooth.adapter.action.SCAN_MODE_CHANGEDandroid.bluetooth.adapter.action.STATE_CHANGEDandroid.bluetooth.device.action.ACL_CONNECTEDandroid.bluetooth.device.action.ACL_DISCONNECTEDandroid.bluetooth.device.action.ACL_DISCONNECT_REQUESTEDandroid.bluetooth.device.action.BOND_STATE_CHANGEDandroid.bluetooth.device.action.CLASS_CHANGEDandroid.bluetooth.device.action.FOUNDandroid.bluetooth.device.action.NAME_CHANGEDandroid.bluetooth.devicepicker.action.DEVICE_SELECTEDandroid.bluetooth.devicepicker.action.LAUNCHandroid.bluetooth.headset.action.AUDIO_STATE_CHANGEDandroid.bluetooth.headset.action.STATE_CHANGED
然后增加的有
Xml代码
- android.intent.action.DOCK_EVENT
- android.provider.Telephony.SMS_REJECTED
android.intent.action.DOCK_EVENTandroid.provider.Telephony.SMS_REJECTED
Api Level 6(SDK 2.0.1)没有改变。
Api Level 7(SDK 2.1)增加的:
Xml代码
- android.intent.action.LOCALE_CHANGED
android.intent.action.LOCALE_CHANGED
Api Level 8(SDK 2.2)增加的:
Xml代码
- android.app.action.ACTION_PASSWORD_CHANGED
- android.app.action.ACTION_PASSWORD_FAILED
- android.app.action.ACTION_PASSWORD_SUCCEEDED
- android.app.action.DEVICE_ADMIN_DISABLED
- android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED
- android.app.action.DEVICE_ADMIN_ENABLED
- android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE
- android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE
- android.media.SCO_AUDIO_STATE_CHANGED
android.app.action.ACTION_PASSWORD_CHANGEDandroid.app.action.ACTION_PASSWORD_FAILEDandroid.app.action.ACTION_PASSWORD_SUCCEEDEDandroid.app.action.DEVICE_ADMIN_DISABLEDandroid.app.action.DEVICE_ADMIN_DISABLE_REQUESTEDandroid.app.action.DEVICE_ADMIN_ENABLEDandroid.intent.action.EXTERNAL_APPLICATIONS_AVAILABLEandroid.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLEandroid.media.SCO_AUDIO_STATE_CHANGED
更多相关文章
- Android(安卓)深入研究SQLite实例(七)
- Android: 用Instrumentation类发送鼠标或按键事件
- android发送QQ邮件(带附件)
- android群发短信时判断短信是否发送成功
- Android(安卓)中使用 ComponentName 类
- Android常用代码
- android 微信登录
- Android之数据存储-手机存储中
- android下联网