【问题标题】:My Alarm Manager does not work in background我的警报管理器无法在后台运行
【发布时间】:2020-08-01 18:13:43
【问题描述】:

我为我的应用程序警报管理器做好了准备。我需要每小时运行一次并检查数据是否更改。

我已经这样设置了警报管理器:

 Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, 1);
    android.app.AlarmManager alarmMgr = (android.app.AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(mContext, AnalysisNotificationReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, ALARM_ID, intent, 0);
    if (Calendar.getInstance().after(cal)) {
        cal.add(Calendar.DAY_OF_MONTH, 1);
    }
    alarmMgr.setRepeating(android.app.AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60 * 60 * 1000, pendingIntent);

它应该每 60 分钟工作一次我不确定我是否设置正确但在应用关闭时不起作用。

有人有想法吗? 谢谢

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    你可以试试这个,它对我有用

    AlarmManager alarmManager= (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
    Intent scheduleServiceExecuterIntent = new Intent(this, ScheduledServiceExecuter.class);
    
    scheduleServiceExecuterIntent.putExtra("state", "Main");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, request_code, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, AlarmManager.INTERVAL_HOUR, pendingIntent);
    

    【讨论】:

    • 不客气。请通过投票支持我。
    • 我做到了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多