我搜索了很多地方,但找不到解决方案。我是Android/Java编程的新手,我正在尝试制作一个应用程序,该应用程序每天(确切时间(从SharedPreferences在SQLite数据库中保存计数器并将其重置为0(如计步器(。
我正在使用AlarmManager和BroadcastReceiver。
我第一次在其中一个活动中使用"setExactAndAllowWhileIdle">设置闹钟,然后 onReceive(( of BroadcastReceiver 我调用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);
}
}
我还有一个扩展应用程序的应用程序类:
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.
(计数器值( at 14:00:00
2.(计数器值( 在 14:03:00
3.(计数器值( 在 14:03:05
4.(计数器值( 在 14:03:10
5.(计数器值( 在 14:03:15
6.(计数器值( 在 14:03:20
7.(计数器值( 在 14:03:25
8.(计数器值(在 14:03:30
...
?.(计数器值(在 14:05:25
任何这方面的帮助将不胜感激!
提前谢谢你!
尝试此操作以重复警报管理器的任务。
private void repeateAlarm() {
Calendar updateTime = Calendar.getInstance();
updateTime.set(alendar.MINUTE, 3);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.cancel(recurringDownload);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), 1000 * 60*3, recurringDownload); // it will run in every 3 min
}