Android,权限拒绝使用BOOT_COMPLETED意图

时间:2012-02-19 23:23:16

标签: android broadcastreceiver alarmmanager

我有一个BootReceiver,正在使用android.intent.action.BOOT_COMPLETED来重新配置用户在我的应用程序中发出的任何警报,然后再关闭电话。

我使用AlarmManager再次设置闹钟。警报存储在SQLite数据库中。

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();

但是当闹钟即将发生时,logcat会给我这个:

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)

到目前为止,我在AlarmReceiver.class中唯一拥有的是:

public class AlarmReceiver extends BroadcastReceiver {

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

我拥有所有适当的权限

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

并设置接收器:

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

我已经看到了这个过程中的成功,但是无法摆脱Permisson否认。我错过了什么?

1 个答案:

答案 0 :(得分: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);

    ...
}

BootReceiver您正在创建PendingIntent并将Intent收到的onReceive(...)传递给您,请查看上面getBroadcast(...)的第三个参数

我猜你的意思是将i作为参数传递而不是intent