java - AlarmManager Dont Work 无法放置清单部分
问题描述
普利斯。我正在制作一个 AlarmManager 用于在 1 分钟内(在后台)重复该动作,但它不起作用,任何人都可以帮助我吗?我的主要活动
int repeatTime = 60; //Repeat alarm time in seconds
AlarmManager processTimer = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, onBackround.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//Repeat alarm every second
processTimer.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),repeatTime*1000, pendingIntent);`
我的广播收到:
package com.example.hatzalahrescatista;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class onBackround extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Do something every 1 MINUTE
}
}`
清单(我不能为此警报管理器提供许可)示例:()使我出现类似“未解决的课程......”之类的错误
```
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".ActividadPrincipal">
<intent-filter>
<action android:name="com.example.hatzalahrescatista_TARGET_NOTIFICATION"></action>
</intent-filter>
</activity>
<activity android:name=".ActividadLogin">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
```
解决方案
根据Android 文档,AlarmManager 是不准确的
这些警报会在空闲时显着影响设备的电源使用(从而导致调度它们的应用程序严重指责电池),因此应谨慎使用它们。为了减少滥用,对特定应用程序的警报响起频率有限制。在正常的系统操作下,它不会超过大约每分钟发送这些警报(此时每个此类挂起的警报都会被发送);在低功耗空闲模式下,此持续时间可能会更长,例如 15 分钟。
因此,如果您希望您的工作每分钟只执行一次,我建议使用Timer
推荐阅读
- python - 如何从下拉菜单访问源
- json - 在 POST 请求期间内容类型标头被覆盖
- javascript - 如何通过 AJAX (Native JS) 调用 ASP.NET WebMethod
- javascript - 如何通过在节点 js 响应中设置标头来自动登录网站
- r - 如果“下标分配中不允许使用 NA”,如何将表的值与 R 中较长的名称列表进行比较和匹配?
- c# - SQL Server 2017 Express 随机插入失败
- javascript - 如何使用 JavaScript 在文件扩展名之前插入文本?
- r - 将收集函数应用于多个 csv 文件,将每个输入转换为 R 中具有相应名称的输出文件
- c - 带有 Windows Mobile 5.0 SDK 的 WIN32 C 程序:使用 time_t 获取链接器错误“未解析的外部”
- java - 随机文字游戏玩法出现“字符串索引越界异常”错误