android.permission.RECEIVE_BOOT_COMPLETED 不需要吗?

发布于 2024-10-10 16:47:24 字数 1332 浏览 6 评论 0原文

有谁知道为什么我的应用程序仍然收到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-10-17 16:47:24

这似乎是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文