gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:05:36 24 4
gpt4 key购买 nike

找了很多地方都没有找到解决办法。我是 Android/Java 编程新手,我正在尝试制作一个应用程序,每天从 SharedPreferences 中保存 SQLite 数据库中的计数器(确切时间)并将其重置为 0(如步数计数器) .

我正在使用 AlarmManager 和 BroadcastReceiver。
我第一次在其中一个 Activity 中使用 "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>

在应用程序的一项 Activity 中启动闹钟:

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.(计数器值)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
}

关于java - Android - AlarmManager 每 5 秒触发一次,而不是 3 分钟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59562310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com