Android,使用 BOOT_COMPLETED 意图拒绝权限
我有一个 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>
我已经看到了在此过程中成功的示例,但无法摆脱许可拒绝。我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
BootReceiver
中,您正在创建一个PendingIntent
并向其传递由onReceive(...)
接收的Intent
> - 查看上面getBroadcast(...)
的第三个参数。我猜您的意思是传递
i
作为该参数而不是intent
。In your
BootReceiver
you are creating aPendingIntent
and passing it theIntent
that is received byonReceive(...)
- look at the third parameter ofgetBroadcast(...)
above.I'm guessing you mean to pass
i
as that parameter rather thanintent
.