Android设置多个重复警报

时间:2015-01-18 17:36:57

标签: android alarmmanager android-pendingintent android-alarms

我想每天重复设置3个不同的闹钟。(如上午9:00至下午1:00至下午6:00) 我为他们创造了按钮。 我点击按钮时可以设置闹钟的时间。 问题是如何更改我的代码以实现上述目的。

private void setNotification() {
    Intent myIntent = new Intent(getActivity(), MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, set_hour);
    calendar.set(Calendar.MINUTE, set_minute);
    calendar.set(Calendar.SECOND,00);
    long startUpTime = calendar.getTimeInMillis();
    System.out.println(startUpTime + "time" + System.currentTimeMillis());
        if (System.currentTimeMillis() > startUpTime) {
            startUpTime = startUpTime + AlarmManager.INTERVAL_DAY;
        }
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, AlarmManager.INTERVAL_DAY , pendingIntent);
}

3 个答案:

答案 0 :(得分:1)

据我所知,您只需点击一下按钮即可设置闹钟。 使用以下两种方法设置警报和取消警报。要重复警报,请在getBroadcast中使用唯一的请求代码,并在取消时使用相同的请求代码值来取消最初用于启动它的警报。

单击该按钮时,调用setAlarm方法。

public void setAlarm(Calendar time,int pk) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, pk, alarmIntent, 0);
        manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
    }


public void cancelAlarm(int pk) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, pk, alarmIntent, 0);
        manager.cancel(pendingIntent);
    }

答案 1 :(得分:0)

<强> Const.java

public class Const {
   public static final int[] ALARM_HOUR_TIME = {9, 9, 9, 9};
   public static final int[] ALARM_MINUTE_TIME = {29, 30, 31, 32};
}

<强> AlarmManagerUtil.java

public class AlarmManagerUtil {

    AlarmManager alarmManager;
    PendingIntent pendingIntent;

    public void initAlarmNotification(Context context) {

        Calendar calendar = getAlarmDate();

        if (calendar == null) {
            return;
        }

        Intent myIntent = new Intent(context, AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);

        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

    public void cancelAlarm(Context context) {
        // If the alarm has been set, cancel it.
        if (alarmManager != null) {
            alarmManager.cancel(pendingIntent);
        }

        // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the
        // alarm when the device is rebooted.
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }

    private Calendar getAlarmDate() {
        Calendar calendar = Calendar.getInstance();

        boolean setAlarm = false;
        int hour = Const.ALARM_HOUR_TIME[0];
        int minute = Const.ALARM_MINUTE_TIME[0];

        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinute = calendar.get(Calendar.MINUTE);

        for (int i = 0; i < Const.ALARM_HOUR_TIME.length; i++) {

            if (currentHour <= Const.ALARM_HOUR_TIME[i] && currentMinute < Const.ALARM_MINUTE_TIME[i] && !setAlarm) {
                hour = Const.ALARM_HOUR_TIME[i];
                minute = Const.ALARM_MINUTE_TIME[i];
                setAlarm = true;
            } else if (i == (Const.ALARM_HOUR_TIME.length - 1) && !setAlarm) {
                calendar.add(Calendar.DATE, 1);
                hour = Const.ALARM_HOUR_TIME[0];
                minute = Const.ALARM_MINUTE_TIME[0];
            }
        }

        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);

        Log.d("MyAlarm", "Next Alarm: " + hour + ":" + minute);

        return calendar;
    }
}

<强> AlarmReceiver.java

public class AlarmReceiver extends WakefulBroadcastReceiver {

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

        AlarmManagerUtil alarmUtil = new AlarmManagerUtil();
        alarmUtil.initAlarmNotification(context);

        createNotification(context, 1);
    }

    private static PendingIntent criarPendingIntent(
            Context ctx, int id) {

        Intent resultIntent = new Intent(ctx, MainActivity.class);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);
        return stackBuilder.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static void createNotification(Context ctx, int id) {

        Bitmap largeIcon = BitmapFactory.decodeResource(
                ctx.getResources(), R.mipmap.ic_launcher);

        PendingIntent pitNotificacao = criarPendingIntent(ctx, id);

        Calendar calendar = Calendar.getInstance();

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My Alarm")
                        .setContentText("HOUR: " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE))
                        .setWhen(System.currentTimeMillis())
                        .setLargeIcon(largeIcon)
                        .setAutoCancel(true)
                        .setContentIntent(pitNotificacao)
                        .setLights(Color.BLUE, 1000, 5000)
                        .setVibrate(new long[]{100, 500, 200, 800})
                        .setNumber(id)
                        .setSubText("GORIO Engenharia");

        NotificationManagerCompat nm = NotificationManagerCompat.from(ctx);
        nm.notify(id, mBuilder.build());
    }
}

答案 2 :(得分:-2)

使用setRepeat或setInExactRepeat方法设置闹钟