【问题标题】:BroadcastReceiver is not called from AlarmManager不从 AlarmManager 调用 BroadcastReceiver
【发布时间】:2019-12-02 17:53:23
【问题描述】:

我不想看到 Oreo 的 AlarmManager setExactAndAllowWhileIdle 是如何工作的,并为此编写了非常简单的服务,但它不起作用,我真的无法理解原因。到目前为止我看到的所有原因都与我的代码无关。请指导我哪里出错了。

主要活动

   private void startService() {
            Log.d("testt", "MainActivity - startService");
            Intent intent = new Intent(this, LocationReceiver.class);
           PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,intent,PendingIntent.FLAG_UPDATE_CURRENT ); 
 AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
                    alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() +  60 * 1000, pendingIntent);
         }

广播接收器

public class LocationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("testt", "LocationReceiver - onReceive");
        Intent i = new Intent(context, LocationService.class);
        context.startService(intent);
    }
}

服务

public class LocationService extends IntentService {
    public LocationService() {
        super("any_name");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d("testt", "LocationService - onHandleIntent");
    }
}

终于显明

    <service android:name=".LocationService" android:exported="false"/>
    <receiver android:name=".LocationReceiver"/>
</application>

【问题讨论】:

    标签: android broadcastreceiver alarmmanager


    【解决方案1】:

    关于您活动中的这行代码:

    alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis() +  60 * 1000, pendingIntent);
    

    您想使用 SystemClock.elapsedRealtime(),而不是 System.currentTimeMillis();您的代码应如下所示:

    alarmManager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +  60 * 1000, pendingIntent);
    

    您需要从设备启动后的毫秒数开始计算间隔时间,而不是自 1970 年 1 月 1 日以来的毫秒数。

    【讨论】:

    【解决方案2】:

    您可以尝试给定的代码。您可以像这样直接调用LocationService或参考此代码Autologout

    Intent myIntent = new Intent(this, LocationService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.MINUTE, 2);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    

    【讨论】:

      【解决方案3】:

      有些时间警报接收器没有调用确切的时间如果它没有调用,请等待一段时间。

      为服务更改您的广播接收器代码

      公共类 LocationReceiver 扩展广播接收器 {

      @Override
      public void onReceive(Context context, Intent intent) {
          Log.d("testt", "LocationReceiver - onReceive");
          Intent i = new Intent(context, LocationService.class);
          context.startForegroundService(intent);
      }}
      

      在您的服务 OnCreate 方法中。 startForeground(11,buildForegroundNotification());

      【讨论】:

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