Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
800 views
in Technique[技术] by (71.8m points)

broadcastreceiver - Is android.permission.RECEIVE_BOOT_COMPLETED not required?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...