android.permission.RECEIVE_BOOT_COMPLETED 不需要吗?
有谁知道为什么我的应用程序仍然收到 ACTION_BOOT_COMPLETED 广播,即使我的应用程序没有清单文件中的权限 android.permission.RECEIVE_BOOT_COMPLETED
?我认为这是必需的,但我使用的一些教程也没有它。有几个做到了。我使用运行 CyanogenMod 的手机进行测试,但我怀疑这是否重要。 LogCat 在每次启动时显示我的“启动通知”日志。请参阅下面的使用代码。
AndroidManifest.xml
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
AlarmReceiver 类
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "MyProgram";
@Override
public void onReceive(Context context, Intent intent) {
try {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d(TAG, "Notified of boot");
}
Intent newIntent = new Intent(context, MyService.class);
context.startService(newIntent);
} catch (Exception e) {
Log.d(TAG, "An alarm was received but there was an error");
e.printStackTrace();
}
}
}
我在模拟器上重新审视了这个,并在 Android 2.1、2.2 和 2.3 上成功重现了“问题”。我收到 ANR(如预期),因为模拟器没有我的应用程序查询的数据库。当我从清单中删除所有声明的使用权限时,我在尝试使用我的应用程序时收到预期的权限拒绝错误。但是,我仍然收到启动时广播的 ACTION_BOOT_COMPLETED 意图。有什么建议吗?
Does anyone know why my application still receives the ACTION_BOOT_COMPLETED broadcast even when my app doesn't have the permission android.permission.RECEIVE_BOOT_COMPLETED
in the manifest file? I thought it was required but a few tutorials I used also didn't have it. A few did. I use my phone running CyanogenMod for testing but I doubt that matters. LogCat shows my "Notified of boot" log upon each boot. See below for code used.
AndroidManifest.xml
<receiver android:name="AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
AlarmReceiver class
public class AlarmReceiver extends BroadcastReceiver {
private static final String TAG = "MyProgram";
@Override
public void onReceive(Context context, Intent intent) {
try {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Log.d(TAG, "Notified of boot");
}
Intent newIntent = new Intent(context, MyService.class);
context.startService(newIntent);
} catch (Exception e) {
Log.d(TAG, "An alarm was received but there was an error");
e.printStackTrace();
}
}
}
I revisited this on the emulator and successfully reproduced the "problem" on Android 2.1, 2.2 and 2.3. I get an ANR (as expected) since the emulator doesn't have the database my app queries. When I remove all declared uses-permissions from the manifest, I get the expected permission denial errors when trying to use my app. However, I still receive the ACTION_BOOT_COMPLETED intent broadcasted upon boot. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是 Android 中的一个错误。我可以在普通 Nexus One 和 Nexus S 硬件上重现该问题。我已就此提交了错误报告。
This would appear to be a bug in Android. I can reproduce the problem on ordinary Nexus One and Nexus S hardware. I have filed a bug report on it.