2012-02-19 89 views
1

我有一個BootReceiver,並且正在使用android.intent.action.BOOT_COMPLETED來重新配置用戶在我的應用程序中在電話關閉前做出的任何警報。Android,使用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> 

我在這看到的exampels與更迭,卻無法擺脫在未經許可的拒絕。我錯過了什麼?

回答

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,並傳遞給它由onReceive(...)收到Intent - 看看上面getBroadcast(...)第三個參數。

我猜你的意思是通過i作爲參數,而不是intent

+2

是的,不知何故,你重播received_boot的意圖,這是給你一個錯誤,因爲這是一個系統意圖。 – Peterdk 2012-02-19 23:59:49

+0

優秀!你當然是對的,用我取代意圖,解決它。 – 2012-02-20 00:10:20

+0

@Jakob:很高興幫助。 – Squonk 2012-02-20 00:30:15