【问题标题】:Alarm Manager Not Working... android.app.ReceiverRestrictedContext警报管理器不工作... android.app.ReceiverRestrictedContext
【发布时间】:2012-01-19 09:01:33
【问题描述】:

我想在特定的时间间隔内从用户定义的类调用对象的方法,例如 2 小时。输入参数是一个 Context 对象。

我使用了我在清单中注册的 AlarmReceiver 广播接收器

<receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>

当发生特定活动时,我正在尝试通过广播接收器启动警报管理器。为了接收警报广播,我创建了alarmReceiver。我需要任何类型的意图过滤器来接收警报意图吗???

我的问题是我想要警报接收器中的应用程序上下文,我尝试使用 AlarmReceiver 的 onReceive 方法的上下文,但它抛出了

    android.app.ReceiverRestrictedContext

错误。

这个错误有什么解决办法吗?如果需要,我将发布我的完整代码...

【问题讨论】:

    标签: android alarmmanager


    【解决方案1】:

    我使用此代码通过广播接收器激活定期警报:

    public class UpdateReceiver extends BroadcastReceiver {
    
        public static final String LOG_TAG = "camerawatch.alarmactivator";
        public static final String ACTION = "de.pribluda.android.camerawatch.UPDATE_ALARM";
    
        public static void activate(Context context) {
            AlarmManager alarmService = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(ACTION);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
            Log.d(LOG_TAG, "pending intent: " + pendingIntent);
            // if no intent there, schedule it ASAP
            if (pendingIntent == null) {
                pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
                // schedule new alarm in 15 minutes
                alarmService.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),300000, pendingIntent);
                Log.d(LOG_TAG, "scheduled intent: " + pendingIntent);
            }
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(LOG_TAG, "received intent: " + intent);
            CameraWidgetProvider.displayCurrentState(context);
        }
    }
    

    注意,激活是在静态方法中完成的,所以我几乎可以从任何地方调用它。 如果且仅当没有此类未决意图时,我会安排警报。至于上下文 - 它几乎在任何地方都可用 - 在广播接收器(服务方法参数)等活动中(它是上下文本身)等。

    PS:广播接收器优于服务,因为它不会一直运行。当服务方法返回时,它会停止并且可能会释放应用程序(除非它执行其他操作,但大多数情况下它会留在后台)。这提供了更长的电池寿命

    【讨论】:

    • 它有效...谢谢...。此外,我在意图中添加了名称,并且我正在使用名称过滤意图...因此无需在意图中指定警报接收器。谢谢:D
    【解决方案2】:
    【解决方案3】:

    你需要这样的东西:

     public RepeatAlarm(Context context, Bundle extras, int timeoutInSeconds){
         AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent intent = new Intent(context, RepeatAlarm.class);
         intent.putExtra(REMINDER_BUNDLE, extras);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
         Calendar time = Calendar.getInstance();
         time.setTimeInMillis(System.currentTimeMillis());
         time.add(Calendar.SECOND, timeoutInSeconds);
         alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),timeoutInSeconds*1000, pendingIntent);
     }
    

    我将应用程序上下文传递给它。

    顺便说一句,您可以在主类中声明一个静态上下文,然后在您需要应用程序上下文时在项目的任何其他类中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多