Android,使用 BOOT_COMPLETED 意图拒绝权限

发布于 2025-01-06 18:27:49 字数 2994 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最丧也最甜 2025-01-13 18:27:49
@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

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文