应用关闭时闹钟无效

时间:2017-02-06 15:35:28

标签: android alarmmanager alarm android-alarms repeatingalarm

当我使用模拟器时,警报工作正常。但是当我尝试使用实际设备时却无法工作。

应用程序打开时的输出。

RTC #8: Alarm{2c1fc9e type 1 when 1486492260454 user.com.hlthee}
    tag=*alarm*:user.com.hlthee/.UpdateTables
    type=1 whenElapsed=+22h43m2s644ms when=2017-02-08 00:01:00
    window=-1 repeatInterval=86400000 count=0
    operation=PendingIntent{7c4e37f: PendingIntentRecord{3f5fbf4c user.com.hlthee broadcastIntent}}

但是当我关闭应用程序时,此条目已删除。为什么?这就是闹钟没响的原因。

警报接收器广播器清单文件。

   <receiver android:name=".UpdateTables"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

请不要说使用服务并在那里注册接收器。我想我在清单中注册接收器它会起作用。并且还可以在模拟器中正常工作。过去3天无法解决此问题。任何帮助都会有所帮助。

我也试过这些方法:

  1. 使用wakefullBroadcastReceiver而不是BroadcastReceiver
  2. 使用setAlarmclock而不是setExact。
  3. 任何人也面临同样的问题。

3 个答案:

答案 0 :(得分:1)

如果您正在使用MI设备,则必须进行一些设置。 转到安全/权限/自动启动/ 然后选择您的应用并启用它。 希望它能奏效。

答案 1 :(得分:1)

实现onTaskRemoved和onDestroy

@Override
public void onTaskRemoved(Intent rootIntent) {
    //Set what to do when task is removed
    super.onTaskRemoved(rootIntent);
}

@Override
public void onDestroy() {
    //What to do when service i destroyed
    super.onDestroy();
}

答案 2 :(得分:1)

如果您正在设置具有该意图的警报管理器且您的应用被强制停止设置: FLAG_INCLUDE_STOPPED_PACKAGES 作为您意图中的标志,则您的问题未得到正确解释... If set, this intent will always match any components in packages that are currently stopped. This is the default behavior when FLAG_EXCLUDE_STOPPED_PACKAGES is not set. If both of these flags are set, this one wins (it allows overriding of exclude for places where the framework may automatically set the exclude flag).

如果你的问题是你没有收到BOOT_COMPLETED这就是我的建议:

第一个:在项目属性中,在清单选项卡下,有一个复选框列表,用于选择要提供的权限,其中一个是RECEIVE_BOOT_COMPLETED。检查是否提供这些权限。

第二次:如果您的应用安装在外部存储设备(SD卡)上,您将永远不会收到启动完成操作。所以你必须在清单标签中指定android:installLocation =“internalOnly”。

第三次:自Android 3.1+版以来,如果用户从未启动过您的应用至少一次或用户“强制关闭”应用程序,则不会收到BOOT_COMPLETE。这样做是为了防止恶意软件自动注册服务。此安全漏洞已在较新版本的Android中关闭。

第四:在您的设备上打开一个adb shell并强制执行此类事件以进行测试: am broadcast -a android.intent.action.BOOT_COMPLETED