我浪费了一整天的时间来寻找报警管理器的解决方案。我现在太累了,无法进一步搜索。我还在stackoverflow中阅读了所有问题及其答案。可以在下面的代码中找出我的错误吗?我非常感谢你。
主要活动
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
Log.e("Testing","ok");
}
}
警报接收器
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.umama.webview">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<!-- <receiver android:process=":remote" android:name="AlarmReceiver"></receiver>-->
<receiver android:name="com.umama.WebView.AndroidAlarmService" android:enabled="true" />
<!--<receiver android:name="net.fusonic.testapp.receivers.TestAlarmReceiver"></receiver>-->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen" />
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".DisplayWebView">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity" />
</application>
</manifest>
舱单
{{1}}
答案 0 :(得分:3)
要获得适用于所有版本的保证,我建议您执行以下代码。也是因为doze。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarms.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, date.getTime(), recurringAlarm);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarms.setExact(AlarmManager.RTC_WAKEUP, date.getTime(), recurringAlarm);
} else {
alarms.set(AlarmManager.RTC_WAKEUP, date.getTime(), recurringAlarm);
}
答案 1 :(得分:1)
尝试用以下字符替换字符串:
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
你必须用1000替换10,因为5 * 10 = 0.05秒是短暂的间隔。
注意Android 5+只能使用1分钟以上的间隔。