BOOT_COMPLETE和ACTION_SHUTDOWN从不叫广播接收器 [英] BOOT_COMPLETE and ACTION_SHUTDOWN never call the BroadcastReceiver
问题描述
我要ACTION_SHUTDOWN和BOOT_COMPLETE利用广播接收器捕捉。
但事实证明,这两个信号从未触发广播接收器(我没有看到任何的logcat日志)。
下面是我的源$ C $ C。
I want to catch ACTION_SHUTDOWN and BOOT_COMPLETE using BroadcastReceiver. But it turns out both signals never trigger the BroadcastReceiver (I didn't see any log on logcat). Here is my source code.
我给清单上的权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
和我尝试这两种方式注册的BroadcastReceiver
and I try to register the BroadcastReceiver in both ways
protected void onCreate(Bundle savedInstanceState)
{
registerReceiver(BootReceiver, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));
registerReceiver(ShutDownReceiver, new IntentFilter(Intent.ACTION_SHUTDOWN));
}
<receiver android:name=".BootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
和源$ C $ C为BootReceiver和ShutDownReceiver如
and the source code for BootReceiver and ShutDownReceiver are as
private BroadcastReceiver BootReceiver = new BroadcastReceiver()
{
private String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTION_BOOT)){
//my stuff
Log.d("Power", "Boot Complete");
}
}
};
private BroadcastReceiver ShutDownReceiver = new BroadcastReceiver()
{
private String ACTION_SHUTDOWN = "android.intent.action.ACTION_SHUTDOWN";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_SHUTDOWN)) {
//my stuff
Log.d("Power", "Shutdown Complete");
}
}
};
另外,我在注销双方的onDestroy BoradcastReceiver
also, I unregister both BoradcastReceiver in onDestroy
public void onDestroy()
{
unregisterReceiver(BootReceiver);
unregisterReceiver(ShutDownReceiver);
super.onDestroy();
}
有谁知道什么地方错了我的code?
或什么我错过?谢谢你。
Does anyone know what's wrong with my code? Or anything I miss? Thank you.
推荐答案
我发现,为什么它没有工作。
由于我使用的是HTC的设备,广播消息是和别人不一样。
I found out why it didn't work. Since I use a HTC device, the broadcast messages are different from others.
- 关闭事件广播
com.htc.intent.action.QUICKBOOT_POWEROFF
- 重新启动(重新启动)事件广播
android.intent.action.ACTION_SHUTDOWN
- 在事件广播力量
com.htc.intent.action.QUICKBOOT_POWERON
在其他设备,关闭设备时,它可能播出的 android.intent.action.QUICKBOOT_POWEROFF
In other device, when shutting down the device, it might broadcast "android.intent.action.QUICKBOOT_POWEROFF
".
这篇关于BOOT_COMPLETE和ACTION_SHUTDOWN从不叫广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!