Android 警报管理器未触发事件

Android Alarm Manager not triggering event

我正在尝试设置 3 个特定时间的闹钟,它们会每天重复。

我有 2 个 Arraylists,一个保存 pendingIntents,另一个保存日历时间。

ArrayList<PendingIntent>intentarray = new ArrayList<PendingIntent>();
ArrayList<Calendar>calTimes = new ArrayList<Calendar>();

我设置了以下时间

    Calendar cal = Calendar.getInstance();
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();

    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY,9);
    cal.set(Calendar.MINUTE,00);
    calTimes.add(cal);

    cal1.setTimeInMillis(System.currentTimeMillis());
    cal1.set(Calendar.HOUR_OF_DAY,15);
    cal1.set(Calendar.MINUTE,00);
    calTimes.add(cal1);

    cal2.setTimeInMillis(System.currentTimeMillis());
    cal2.set(Calendar.HOUR_OF_DAY,21);
    cal2.set(Calendar.MINUTE,00);
    calTimes.add(cal2);

后跟一个数组来存放 AlarmManagers

AlarmManager[] alarmManager=new AlarmManager[3];

然后在一个循环中,我将每个警报分配给警报管理器。

for(int i =0; i < calTimes.size(); i++){

        Intent alarmIntent = new Intent(this,AlarmReceiver.class);
        pendingIntent = pendingIntent.getBroadcast(this,i,alarmIntent,0);

        alarmManager[i] = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        alarmManager[i].setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

        intentarray.add(pendingIntent);
    }

`

当我运行应用程序并更改触发警报的时间时。什么都没发生。

这是我的 BroadcastReceiver。

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"starting App",Toast.LENGTH_LONG).show();
        startApp(context,"com.example.myapp");
    }

    public void startApp(Context context, String packageName){
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent != null){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}

您正在向 AlarmManager 添加 Intent。您需要添加一个 PendingIntent.

替换

alarmManager[i].setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

alarmManager[i].setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

另一件事:您不需要使用 AlarmManager 对象数组来添加重复警报。 AlarmManager 的单个实例即可。

如果你想设置很多闹钟,你必须给你的意图不同的id(否则新的闹钟会取代以前的闹钟)。

for(int i =0; i < calTimes.size(); i++){

    Intent alarmIntent = new Intent(this,AlarmReceiver.class);
    intent.setData(Uri.parse("timer:" + i));
    pendingIntent = pendingIntent.getBroadcast(this,i,alarmIntent,0);

    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY,alarmIntent);

    intentarray.add(pendingIntent);
}

这个有效:

AlarmManager alarmManager;


alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


 private void alarms(){

        ArrayList<Integer> intentarray = new ArrayList<>();
        ArrayList<Calendar> calTimes = new ArrayList<>();

        Calendar cal = Calendar.getInstance();
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();

        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, 9);
        cal.set(Calendar.MINUTE, 00);
        calTimes.add(cal);

        cal1.setTimeInMillis(System.currentTimeMillis());
        cal1.set(Calendar.HOUR_OF_DAY, 15);
        cal1.set(Calendar.MINUTE, 00);
        calTimes.add(cal1);

        cal2.setTimeInMillis(System.currentTimeMillis());
        cal2.set(Calendar.HOUR_OF_DAY, 21);
        cal2.set(Calendar.MINUTE,00);
        calTimes.add(cal2);


        Intent alarmIntent = new Intent(this, AlarmReceiver.class);

        for(int i = 0; i < calTimes.size(); i++){

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i, alarmIntent,0);

            //Wake up the device to fire the alarm at approximately
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calTimes.get(i).getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
            intentarray.add(i);
        }
    }

已编辑:

如果你需要在不使用 startActivity 的情况下唤醒你的 Activity,你需要这样的东西:

<uses-permission android:name="android.permission.WAKE_LOCK"/>


import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

public class AlarmReceiver extends WakefulBroadcastReceiver {

    public static PowerManager.WakeLock wakeLock;

    public AlarmReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
        wakeLock.acquire();

        Toast.makeText(context, "starting App", Toast.LENGTH_LONG).show();
        startApp(context,"com.example.myapp");

        wakeLock.release();
    }

    public void startApp(Context context, String packageName){

        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);

        if (intent != null){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}