Android-AlarmManager每5秒触发一次,而不是3分钟触发一次

问题描述 投票:0回答:1

我已经搜索了很多地方,但找不到解决方案。我是Android / Java编程的新手,我正在尝试制作一个应用程序,该应用程序每天(确切时间)从SharedPreferences中将计数器保存在SQLite数据库中,然后将其重置为0(如步进计数器)。我正在使用AlarmManager和BroadcastReceiver。我第一次在其中一个活动中使用“ setExactAndAllowWhileIdle”设置警报,然后使用BroadcastReceiver的onReceive()调用restartAlarm()方法重新安排警报。问题闹钟在特定时间(例如14:00:00)首次正确触发。也可以在3分钟(14:03:00)后重新安排并正确触发,但只有一次。

我面临的问题是经过重新安排的警报在3分钟后首次触发(来自BroadcastReceiver中的restartAlarm()方法),然后警报每5秒而不是3分钟触发一次(我想每天重复一次警报,但我每3分钟设置一次警报[[仅用于测试结果)。这就是我在做什么:我在Manifest.xml文件中设置权限和接收者]<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:name=".ApplicationClass" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> .... <receiver android:name=".MyAlarmReceiver" android:directBootAware="true" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" /> </intent-filter> </receiver> </application>

在应用程序的一项活动中启动警报:

public class CompleteRegistered extends AppCompatActivity { ... public static AlarmManager alarmManager; public static PendingIntent pendingIntent; private static final int GLASS_REQUEST_CODE = 101; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_complete_registered); ... startAlarmManager(); } private void startAlarmManager(){ // Creating the pending intent to send to the BroadcastReceiver alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent alarmIntent = new Intent(getApplicationContext(), MyAlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(this, GLASS_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); // Setting the specific time for the alarm manager to trigger the intent Calendar startTime = Calendar.getInstance(); startTime.setTimeInMillis(System.currentTimeMillis()); startTime.set(Calendar.HOUR_OF_DAY, 14); startTime.set(Calendar.MINUTE, 0); startTime.set(Calendar.SECOND, 0); // Starts the alarm manager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent); } }

保存到SQLite数据库的计数器并在BroadcastReceiver中重新安排警报:

public class MyAlarmReceiver extends BroadcastReceiver { private static final String MyPREFERENCES = "MyPrefs" ; private SharedPreferences sharedpreferences; public static AlarmManager alarmManager; public static PendingIntent pendingIntent; public static Intent alarmIntent; private static final int GLASS_REQUEST_CODE = 102; // Database Helper private DatabaseHelper db; public void restartAlarm(Context context){ // Creating the pending intent to send to the BroadcastReceiver alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmIntent = new Intent(context, MyAlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(context, GLASS_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); // Setting the specific time for the alarm manager to trigger the intent Calendar startTime = Calendar.getInstance(); startTime.setTimeInMillis(System.currentTimeMillis()); startTime.set(Calendar.HOUR_OF_DAY, 14); startTime.set(Calendar.MINUTE, 0); startTime.set(Calendar.SECOND, 0); // Getting time Calendar now = Calendar.getInstance(); if (now.after(startTime)){ // startTime.add(Calendar.DATE, 1); startTime.add(Calendar.MINUTE, 3); } // Starts the alarm manager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { alarmManager.setExact(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent); } else { alarmManager.set(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent); } } @Override public void onReceive(Context context, Intent intent) { // When device reboots if (intent.getAction() == Intent.ACTION_BOOT_COMPLETED) { // Set the alarm. startAlarm(context); } else { sharedpreferences = context.getSharedPreferences(MyPREFERENCES, Activity.MODE_PRIVATE); final SharedPreferences.Editor editor = sharedpreferences.edit(); // Get glasses from SharedPreferences int glassCounter = sharedpreferences.getInt("glasses", 0); // Create a WaterTracker WaterTracker waterTracker = new WaterTracker(); waterTracker.setGlasses(glassCounter); // Store to SQLite database db = new DatabaseHelper(context); db.createWaterTracker(waterTracker); // Reset glasses from SharedPreferences editor.putInt("glasses", 0); editor.apply(); // Set the alarm again. restartAlarm(context); } }

而且我还有一个扩展应用程序的ApplicationClass:

public class ApplicationClass extends Application { ... public static AlarmManager alarmManager; public static PendingIntent pendingIntent; private static final int GLASS_REQUEST_CODE = 101; @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onCreate() { super.onCreate(); ... ComponentName receiver = new ComponentName(getApplicationContext(), MyAlarmReceiver.class); PackageManager pm = getApplicationContext().getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } }

我从这段代码中得到的结果是:1.(计数器值)在14:00:002.(计数器值)在14:03:003.(计数器值)在14:03:054.(计数器值)在14:03:105.(计数器值)在14:03:156.(计数器值)在14:03:207.(计数器值)在14:03:258.(计数器值)在14:03:30...? (计数器值)在14:05:25

任何帮助,我们将不胜感激!预先谢谢!

我已经搜索了很多地方,但找不到解决方案。我是Android / Java编程的新手,我正在尝试制作一个每天从SharedPreferences中将计数器保存在SQLite数据库中的应用(...

java android broadcastreceiver alarmmanager repeatingalarm
1个回答
0
投票
尝试此操作以重复执行警报管理器。
© www.soinside.com 2019 - 2024. All rights reserved.