Android, permission denial using BOOT_COMPLETED in

2019-05-30 04:30发布

问题:

I have a BootReceiver and is using android.intent.action.BOOT_COMPLETED to reconfigure whatever alarms the user made in my app, before phone was powerd off.

Im using AlarmManager to set the alarms again. The alarms are stored in a SQLite database.

public class BootReceiver extends BroadcastReceiver {

private Context mContext;   

@Override
public void onReceive(Context c, Intent intent) {
    mContext = c;

try {
        cursor.moveToFirst();
        do {
            Intent i = new Intent(mContext, AlarmReceiver.class);
            i.putExtra("alarm_message", cursor.getInt(1));
            PendingIntent pendingIntent  = PendingIntent.getBroadcast(mContext, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);
            AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, cursor.getLong(4), pendingIntent);
            Log.e("DB", "Future alarms was set after reboot");
        } while (cursor.moveToNext());
    } catch (Exception e1) {
        Log.e("DB", "DB is empty");
    }
    cursor.close();

But when alarm is about to occur, the logcat gives me this:

Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=-1, uid=10067
Unable to send startActivity intent
W/ActivityManager(1584): java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=-1, uid=10067
02-19 23:42:26.989: W/ActivityManager(1584):    at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:10310)
02-19 23:42:26.989: W/ActivityManager(1584):    at com.android.server.am.ActivityManagerService.broadcastIntentInPackage(ActivityManagerService.java:10587)
02-19 23:42:26.989: W/ActivityManager(1584):    at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:228)
02-19 23:42:26.989: W/ActivityManager(1584):    at com.android.server.am.PendingIntentRecord.send(PendingIntentRecord.java:177)
02-19 23:42:26.989: W/ActivityManager(1584):    at android.app.PendingIntent.send(PendingIntent.java:400)
02-19 23:42:26.989: W/ActivityManager(1584):    at com.android.server.AlarmManagerService$AlarmThread.run(AlarmManagerService.java:692)

So far, the only thing I have in my AlarmReceiver.class is:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("AlarmReceiver", "Alarm received");

I have all the proper permissions

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />

and set the receivers:

<receiver android:name=".AlarmReceiver"></receiver>
<receiver android:name=".BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

I've seen exampels of during this with succes, but cannot get rid of that Permisson denial. What am I missing?

回答1:

@Override
public void onReceive(Context c, Intent intent) {

    ...

    Intent i = new Intent(mContext, AlarmReceiver.class);
    i.putExtra("alarm_message", cursor.getInt(1));
    PendingIntent pendingIntent  = PendingIntent.getBroadcast(mContext, (int)System.currentTimeMillis(), intent, PendingIntent.FLAG_ONE_SHOT);

    ...
}

In your BootReceiver you are creating a PendingIntent and passing it the Intent that is received by onReceive(...) - look at the third parameter of getBroadcast(...) above.

I'm guessing you mean to pass i as that parameter rather than intent.