Android Alarm Manager不会触发事件

时间:2015-03-30 12:50:52

标签: android android-intent alarmmanager android-pendingintent android-broadcast

我试图设置3个特定时间的闹钟,并且每天都会重复这些闹钟。

我有2个Arraylists,一个持有pendingIntents,另一个持有Calendar时间。

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);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您要向Intent添加AlarmManager。您需要添加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的单个实例可以。

答案 1 :(得分:0)

如果要设置许多警报,则必须为您的意图指定不同的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);
}

答案 2 :(得分:0)

这有效:

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);
        }
    }
}