当前位置: 首页 > 知识库问答 >
问题:

AlarmManager尽快而不是在设定的时间调用intent服务

公西财
2023-03-14

我有一个setExact警报设置在我的应用程序,当它达到那个点,而不是激活警报在指定的时间,它激活它尽快!

我的alarmmanager代码

@TargetApi(Build.VERSION_CODES.M)
public void setUpAlarmManager(long time) {

    final Intent intentAlarm = new Intent(this, AlarmService.class);
    final PendingIntent pendingIntent = PendingIntent.getService(this, 100, intentAlarm, PendingIntent.FLAG_ONE_SHOT);
    final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            alarmManager.cancel(pendingIntent);
        }
    }, 1000);
    Log.i(TAG, "setUpAlarmManager: " + time);
}
public class AlarmService extends IntentService {

private static final String TAG = "Tag";

// private Ringtone ringtoneAlarm;

public AlarmService() {
    super("AlarmService");
//     ringtoneAlarm = null;
    Log.i(TAG, "AlarmService: ");
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i(TAG, "onHandleIntent: ");
    Uri alarmTone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtoneAlarm = RingtoneManager.getRingtone(getApplicationContext(), alarmTone);
    ringtoneAlarm.play();
}

@Override
public void onDestroy() {
    Log.i(TAG, "onDestroy: ");
    super.onDestroy();
//    ringtoneAlarm.stop();
}


}

共有1个答案

濮阳宏硕
2023-03-14

要将警报设置为从现在起的32秒,您可以这样做:

private void setAlarmToFuthure(long time){
        Calendar cal_alarm = Calendar.getInstance();
        cal_alarm.add(Calendar.SECOND, (int)time); // Here you add the desired value to the current time (for example: 32 seconds)

        final Intent intentAlarm = new Intent(this, AlarmService.class);
        final PendingIntent pendingIntent = PendingIntent.getService(this, 100, intentAlarm, PendingIntent.FLAG_ONE_SHOT);
        final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(), pendingIntent);
    }
 类似资料:
  • 我正在使用AlarmManager,如下所示,它在Android O及以下版本中运行良好。 但在Android P中,我在Logcat中得到以下错误: 不允许后台启动:服务意图{flg=0x4 cmp=com.app.appname/.PeriodicChecksService(具有额外功能)}到com。应用程序。记录器/。来自pid的PeriodicChecksService=-1 uid=10

  • 问题内容: 我如何获得客户的时间?当我使用它返回服务器的时间。 问题答案: 每个人都提到,PHP仅显示服务器端时间。 对于客户端,您将需要Javascript,以下方法可以解决问题。 而且,如果您想要AM / PM后缀,则应执行以下操作:

  • 下面的函数在服务中被调用。当条件得到满足时,意图就是要求做某事。我使用了

  • 本文向大家介绍Shell脚本调快调慢系统时间(测试服务器时使用),包括了Shell脚本调快调慢系统时间(测试服务器时使用)的使用技巧和注意事项,需要的朋友参考一下 公司近来服务器测试,需要经常调整系统时间,一两台还好半,稍微多点,就各种纠结了,笔者这几台都把笔者快弄疯了。老大一句话:全部调快3分钟...过会又全部调慢5分钟......然后咱们干活的,各种date 01021511....你懂的.于

  • 在php.ini我设置了。重新启动了Apache,中的输出显示了正确的时区(Europe/Amsterdam)。现在当我打印时,我得到的是UTC时间,比我们的本地时间晚2小时。但是当我打印时,我仍然得到UTC时间,而我期望的是本地时间。 的输出是: 如果您能帮我了解当地时间,我们将不胜感激。 更新: phpinfo()的输出为: 所以“date.timezone”是正确的,但与“默认时区”有什么区

  • 我正在尝试使用Intent打开文件。操作\u获取\u类似于此帖子的内容: Android打开一个文件与ACTION_GET_CONTENT结果到不同的Uri的 但是这里只是一个解决方案,如何使用不同的SDK/文件夹获取文件名,而不是不同的设备。获得Uri的意图也保持不变。 我想开门。png文件。 Uri.getPath()为两个设备(都. png文件存储在下载文件夹): 因此,问题是,如果我使用