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

时间:2020-01-02 11:08:54

标签: java android broadcastreceiver alarmmanager repeatingalarm

我搜索了很多地方,但找不到解决方案。我是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: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

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

1 个答案:

答案 0 :(得分:0)

尝试此操作以重复执行警报管理器。

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
}