AlarmManager的setRepeating仅是第一次触发,不会重复

时间:2019-03-11 14:58:14

标签: java android alarmmanager

我正在尝试实现一个警报管理器,每天都会调用它来更新数据库。我首先尝试了alarmManager的设置功能,它在特定时间触发了警报。然后,我继续尝试每天使用setRepeating触发警报。仅为了测试而执行的每分钟重复一次的setRepeating函数仅在第一次工作,并且没有重复执行该函数。

这是我的BroadcastReceiver:

package com.example.bleh.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bleh.myapplication.DB.AppDatabase;
import com.example.bleh.myapplication.DB.Plan;
import com.example.bleh.myapplication.DB.User;
import com.example.bleh.myapplication.Utils1.FormulaUtils;
import com.github.lzyzsd.circleprogress.DonutProgress;

public class AlarmReceiver extends BroadcastReceiver {

    private static final String DEBUG_TAG = "AlarmReceiver";
    public AppDatabase mydb;
    Plan plan;

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
        mydb = AppDatabase.getInstance(context);
        final long planid = intent.getExtras().getLong("planid");
        final long userid = intent.getExtras().getLong("uid");
        final String requirements = intent.getExtras().getString("requirements");
        Log.wtf("PlanId: ",planid+"");
        Log.wtf("UserId: ",userid+"");
        Log.wtf("Requirements",requirements);
        plan = mydb.getPlanDao(context).getPlanById((int) planid);
        final User user = mydb.getUserDao(context).getUserById((int) userid);
        plan.setCurrentWeight(FormulaUtils.reCalculateWeight(plan.getCurrentWeight(), Double.parseDouble(requirements)));
        plan.setBmr(Double.parseDouble(FormulaUtils.calculateBmr(user.getSex(), plan.getCurrentWeight(), user.getHeight(), user.getBirthDay())));
        plan.setNbOfDays(plan.getNbOfDays() - 1);
        mydb.getPlanDao(context).update(plan);
        String requirement = FormulaUtils.CalulcateDailyRequirements(plan.getWorkoutPerWeek(), plan.getBmr());
        String day = plan.getNbOfDays() + "";
        float progress = 0F;

        // Start feature2 activity with updated data
        Intent updateFeature2Intent = new Intent(context, feature2.class);
        updateFeature2Intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add this flag
        updateFeature2Intent.putExtra("requirement", requirement);
        updateFeature2Intent.putExtra("day", day);
        updateFeature2Intent.putExtra("progress", progress);
        context.startActivity(updateFeature2Intent);
    }

}

这是我的专题活动:

package com.example.bleh.myapplication;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.bleh.myapplication.DB.AppDatabase;
import com.example.bleh.myapplication.DB.Exercise;
import com.example.bleh.myapplication.DB.Food;
import com.example.bleh.myapplication.DB.Plan;
import com.example.bleh.myapplication.DB.User;
import com.example.bleh.myapplication.Utils1.FormulaUtils;
import com.github.lzyzsd.circleprogress.DonutProgress;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class feature2 extends AppCompatActivity {

    public AppDatabase mydb;
    TextView BMR,requirements,days;
    Button addfood,addex,nextday;
    LinearLayout mainLayout;
    Button Meas,Bluetooth;
    DonutProgress donutProgress;
    Plan plan;

    // Add this method
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // Get data from intent
        String requirement = intent.getStringExtra("requirement");
        String day = intent.getStringExtra("day");
        float progress = intent.getFloatExtra("progress", 0F);

        // Update UI
        TextView requirements = findViewById(R.id.requirements);
        TextView Days = findViewById(R.id.days);
        DonutProgress dailyProgress = findViewById(R.id.donut_progress);

        requirements.setText(requirement);
        Days.setText(day);
        dailyProgress.setProgress(progress);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feature2);
        Intent intent = getIntent();
        requirements = findViewById(R.id.requirements);
        donutProgress = findViewById(R.id.donut_progress);
        days = findViewById(R.id.days);
        final long planid = intent.getExtras().getLong("planid");
        final long userid = intent.getExtras().getLong("uid");
        mydb = AppDatabase.getInstance(feature2.this);
        plan = mydb.getPlanDao(feature2.this).getPlanById((int) planid);
        try {
            requirements.setText(FormulaUtils.CalulcateDailyRequirements(plan.getWorkoutPerWeek(), plan.getBmr()));
        }
        catch (Exception ex)
        {
            Log.wtf("There is no plan","!");
        }
        Intent intent1 = new Intent(this, AlarmReceiver.class);
        intent1.putExtra("uid", userid);
        intent1.putExtra("planid", planid);
        intent1.putExtra("requirements",requirements.getText().toString());
        Calendar updateTime = Calendar.getInstance();
        updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
        updateTime.set(Calendar.HOUR_OF_DAY, 8);
        updateTime.set(Calendar.MINUTE, 3);
        updateTime.set(Calendar.SECOND,0);
        Date milliseconds = updateTime.getTime();
        long millis = milliseconds.getTime();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                intent1, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, millis ,60000, pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
    }
}

这是我的清单部分:

<receiver android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="true">
        </receiver>

2 个答案:

答案 0 :(得分:0)

我之前在我的应用中遇到了此问题
如果手机处于空闲状态,则api> 23时不会触发重复警报。请参阅Schedue Repeating alarms
打ze模式也会阻止警报。

您可以做的是对 api> 23 使用 setExact setExactAndAllowWhileIdle 并使用自己的逻辑来重复警报。

>
if (Build.VERSION.SDK_INT >= 23) {
      alarmManager.setExactAndAllowWhileIdle(RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
} else {
      alarmManager.setExact(RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}

在接收器或警报管理器触发的活动中再次触发警报。

对于您的情况,第二天在AlarmReceiver中再次设置警报。

答案 1 :(得分:0)

我建议您阅读有关Doze模式和此topic的信息。您也可以使用Evernote的lib。它有助于处理后台任务的调度。