1

我正在与硬件通信蓝牙设备。我想在特定时间将数据发送到蓝牙模块,这是安排通话功能的最佳方法,我应该使用警报管理器或作业调度程序。

4

2 回答 2

2

你应该报警管理器。您将无法使用 Jobscheduler 进行控制。JobScheduler 中的计划作业将根据您无法影响的操作系统定义的标准执行。如果您的用例需要在特定时间执行,那么 AlarmManager 应该是您的选择。

根据文档

标准 AlarmManager 警报(包括 setExact() 和 setWindow())被推迟到下一个维护窗口。

  • 如果您需要设置在打瞌睡时触发的警报,请使用 setAndAllowWhileIdle() 或 setExactAndAllowWhileIdle()。
  • 使用 setAlarmClock() 设置的警报继续正常触发——系统在这些警报触发前不久退出打盹。
于 2018-05-10T09:50:45.967 回答
0

它取决于您的任务,如果您想在应用程序被杀死时执行任务以及处于打盹模式的设备,那么请使用带有作业调度程序的警报管理器,否则您可以使用作业调度程序。

这是安排警报的正确方法。

public static void startAlarm(Context context, int minutes) {
        Logger.print("AlarmReceiver startAlarm  called");
        Intent alarmIntent = new Intent(context, WakefulBroadcastReceiverService.class);
        alarmIntent.setAction("testAPP");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123451, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        manager.cancel(pendingIntent);
        long alarmPeriodicTime = System.currentTimeMillis() + Utils.getTimeInMilliSec(Constant.TimeType.MINUTE, minutes);
        if (Build.VERSION.SDK_INT >= 23) {
            manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
        } else if (Build.VERSION.SDK_INT >= 19) {
            manager.setExact(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
        } else {
            manager.set(AlarmManager.RTC_WAKEUP, alarmPeriodicTime, pendingIntent);
        }
    }

对于作业调度程序用户,请使用 FirebaseJob 调度程序以获得较低版本的支持

于 2018-05-10T10:03:45.580 回答