第一种方法:
1.查找需要的Broadcast,见附录1
2.在AndroidManefest.xml里添加<receiver>...<receiver/>
<receiver android:name=".BootComplementReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
3.继承BroadcastReceiver
public class BootComplementReceiver extends BroadcastReceiver {
private static final String TAG = "BootComplementReceiver";
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "onReceive");
String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.v(TAG, "action = " + action);
}
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)
|| action.equals(Intent.ACTION_TIME_CHANGED)) {
Log.v(TAG, "action = " + action);
}
}
}
附录1:
android.bluetooth.intent.action.BONDING_CREATED
android.bluetooth.intent.action.BONDING_REMOVED
android.bluetooth.intent.action.DISABLED
android.bluetooth.intent.action.DISCOVERY_COMPLETED
android.bluetooth.intent.action.DISCOVERY_STARTED
android.bluetooth.intent.action.ENABLED
android.bluetooth.intent.action.HEADSET_STATE_CHANGED
android.bluetooth.intent.action.MODE_CHANGED
android.bluetooth.intent.action.NAME_CHANGED
android.bluetooth.intent.action.PAIRING_CANCEL
android.bluetooth.intent.action.PAIRING_REQUEST
android.bluetooth.intent.action.REMOTE_ALIAS_CHANGED
android.bluetooth.intent.action.REMOTE_ALIAS_CLEARED
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.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.MANAGE_PACKAGE_STORAGE
android.intent.action.MEDIA_BAD_REMOVAL
android.intent.action.MEDIA_BUTTON
android.intent.action.MEDIA_EJECT
android.intent.action.MEDIA_MOUNTED
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_INSTALL
android.intent.action.PACKAGE_REMOVED
android.intent.action.PACKAGE_RESTARTED
android.intent.action.POWER_CONNECTED
android.intent.action.POWER_DISCONNECTED
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.WALLPAPER_CHANGED
android.media.RINGER_MODE_CHANGED
android.media.VIBRATE_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
第二种方法:
1. 继承BroadcastReceiver,实现onReceive方法。
2. 注册该BroadcastReceiver。
3. 最后销毁该BroadcastReceiver。
分享到:
相关推荐
Intent的ACTION字段用于指定广播的类型,EXTRA字段则可以添加额外的信息。 在使用BroadcastReceiver时,需要注意一些最佳实践。比如,尽量减少动态注册,因为这会增加内存消耗;如果接收器需要处理耗时的操作,应该...
扫描结果可通过`BroadcastReceiver`监听`ACTION_DISCOVERY_STARTED`和`ACTION_DISCOVERY_FINISHED`广播,以及设备发现的`ACTION_FOUND`广播: ```java IntentFilter filter = new IntentFilter(); filter.addAction...
当手机接收到电话呼入时,系统会发送一个ACTION_PHONE_STATE_CHANGED的广播,我们可以注册一个BroadcastReceiver监听这个广播,以便在电话状态改变时进行相应处理。 在`AndroidManifest.xml`文件中,我们需要声明...
ACTION_INPUT_METHOD_CHANGED 当输入法发生变化时,会触发这个广播。应用程序可以利用这个广播来适应新的输入环境,例如调整键盘布局。 了解并正确处理这些广播对于构建响应迅速、用户友好的Android应用程序至关...
16. **Intent.ACTION_INPUT_METHOD_CHANGED**:当用户切换了输入法时,系统会发送此广播,让应用知道当前的输入法。 17. **Intent.ACTION_LOCALE_CHANGED**:当设备的区域设置(语言和地区)改变时,系统会发送这个...
2. **BroadcastReceiver**:广播接收器用于监听系统事件,比如电源状态改变。当手机被开机或重启时,可以通过注册对`ACTION_BOOT_COMPLETED`广播的监听,来触发自启服务的启动。 3. **权限申请**:在...
__android_log_print(ANDROID_LOG_INFO, "NativeHelper", "Sending SMS to: %s, Message: %s", recipientChars, messageChars); // 使用Android SMS API发送短信 // ... 这里需要使用Java层的SMSManager类,因此...
if (intent.getAction().equals(Intent.ACTION_INPUT_METHOD_CHANGED)) { if (intent.getBooleanExtra("inputMethodChanged", false)) { if (getResources().getConfiguration().keyboardHidden == Configuration...
Class<?> telephonyClass = Class.forName("android.telephony.TelephonyManager"); Method getITelephonyMethod = telephonyClass.getDeclaredMethod("getITelephony"); getITelephonyMethod.setAccessible(true); ...
这个例子主要研究Android broadcast广播机制的实现,以一个电池监测及广播通知为例,介绍了实现broadcast的过程:大致的思路是:注册一个系统 BroadcastReceiver,作为访问电池计量之用,如果捕捉到的action是ACTION...
Method answerCallMethod = telephonyClass.getDeclaredMethod("answerRingingCall", int.class); answerCallMethod.invoke(telephonyService, 0); // 0表示无静音接听 ``` 最后,为了用户体验,可能还需要添加一些...
<action android:name="com.baidu.android.pushservice.action.METHOD" /> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <action android:name="android.intent.action.BOOT_...
if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (state.equals(TelephonyManager.EXTRA_STATE_...
- **BroadcastReceiver**:对于某些特定的系统级按键(如电源键),开发者可能需要注册BroadcastReceiver监听ACTION_BOOT_COMPLETED和ACTION_POWER_CONNECTED等广播,以便在特定情况下执行操作。 2. **触摸屏事件...
private final BroadcastReceiver mFindBlueToothReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // When ...
<action android:name="com.baidu.android.pushservice.action.METHOD" /> <action android:name="com.baidu.android.pushservice.action.MESSAGE" /> <action android:name="android.intent.action.NETWORK_...
Intent intent = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN"); intent.putExtra("reason", "App initiated shutdown"); PendingIntent shutdownIntent = PendingIntent.getBroadcast(context, 0, ...
Method answerCallMethod = telephonyClass.getDeclaredMethod("answerRingingCall"); answerCallMethod.setAccessible(true); answerCallMethod.invoke(telephonyService); ``` 为了允许用户控制这个服务,我们...
<action android:name="android.intent.action.BOOT_COMPLETED"/> ``` 完成上述步骤后,当设备启动时,`LatinImeReceiver`会自动运行,根据指定的代码设置默认输入语言为英语、缅甸语和法语。需要注意的是...