【问题标题】:How schedule repeat alarm and one time alarm in android如何在android中安排重复闹钟和一次闹钟
【发布时间】:2017-03-07 05:34:10
【问题描述】:

我的应用程序包含两种方法,即一次闹钟和每天重复闹钟,我需要根据我的要求设置闹钟。当我设置一个时间警报时它工作正常,但当我设置重复警报时它不工作。我也研究过以下方法,但它确实对我有用。

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour); // hour - user selected hour
calendar.set(Calendar.MINUTE, minute);  // minute -user selected minute
// setRepeating() lets you specify a precise custom interval--in this case,
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    AlarmManager.INTERVAL_DAY, alarmIntent);

这是我的片段中一次重复警报的方法。

 private SimpleDateFormat EEE_DD_MMM_YYYY_HH_MM_SS_A_Formatter = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss a"); //Tue, 07 Mar 2017 10:50:00 a.m.
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getBaseContext(), alarmId, intent, 0); // where alarmId is autoIncrement 
    AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
    try {
        Date alarmDate = EEE_DD_MMM_YYYY_HH_MM_SS_A_Formatter.parse(getDateTime()); // where we get both time and date from both pickers respectively.
        if (oneTimeAlarm) { 
        Logs.i(TAG,"one time alarm");
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTime(), pendingIntent);
        Log.i(TAG,"one time alarm set!");
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmDate.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);
            Log.i(TAG,"repeat alarm set!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这是我的报警接收器类文件

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Vibrator vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);    //for Vibration
    vib.vibrate(2000);
 showNotification(context);
}

private void showNotification(Context context) {
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context,alarmId,intent, 0);

    //  NotificationCompat
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Title")
                    .setContentText("text");
    mBuilder.setContentIntent(contentIntent);
    mBuilder.setDefaults(Notification.DEFAULT_SOUND);
    mBuilder.setAutoCancel(true);
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());}}

【问题讨论】:

    标签: android repeatingalarm


    【解决方案1】:

    试试这个。我正在使用它,它工作正常。并使用 Service 或 Intent 服务来抛出不使用 Brodcast 的通知。

        AlarmManager al;
        PendingIntent fintent;
        Calendar calendar;
        Intent notif;
        long _alarm;
    
    
    
    
        Calendar now = Calendar.getInstance();
            Calendar wakeupcall = Calendar.getInstance();
            wakeupcall.setTimeInMillis(System.currentTimeMillis());
            wakeupcall.set(Calendar.HOUR_OF_DAY, 12);
            wakeupcall.set(Calendar.MINUTE, 30);
    
            if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
                _alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
            else
                _alarm=wakeupcall.getTimeInMillis();
    
    al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            notif= new Intent(this,TestNotifyService.class);
            fintent = PendingIntent.getService(this,0,notif,0);
    
            if (SDK_INT < Build.VERSION_CODES.KITKAT) {
                al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
    
            }
            else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
                al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
    
            }
            else if (SDK_INT >= Build.VERSION_CODES.M) {
                al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
    
            }
    

    如果您想从服务器获取数据然后发布通知,请使用电源管理器和唤醒日志成功显示通知。并且不要忘记在清单中添加 WAKE LOG 和 SET ALARM 权限。

    创建通知后重置警报:

    private void reSETNOTIFY() {
    
            Calendar now = Calendar.getInstance();
            Calendar wakeupcall = Calendar.getInstance();
            wakeupcall.setTimeInMillis(System.currentTimeMillis());
            wakeupcall.set(Calendar.HOUR_OF_DAY, 12);
            wakeupcall.set(Calendar.MINUTE, 29);
    
            if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
                _alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1)+25000;
            else
                _alarm=wakeupcall.getTimeInMillis()+25000;
    
            al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            notif= new Intent(this,TestNotifyService.class);
            fintent = PendingIntent.getService(this,0,notif,0);
    
            if (SDK_INT < Build.VERSION_CODES.KITKAT) {
                al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
    
            }
            else if (Build.VERSION_CODES.KITKAT <= SDK_INT  && SDK_INT < Build.VERSION_CODES.M) {
                al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
    
            }
            else if (SDK_INT >= Build.VERSION_CODES.M) {
                al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
    
            }
        }
    

    【讨论】:

    • 以上代码火警12:30,然后您可以在每天发出通知上班后在您的服务中使用相同的代码
    • 我已经替换了代码,警报已在当天触发,但是当我更改日期时(比如说明天)警报没有触发。
    • 您是否将上述代码放入您的服务或广播中?
    • 这太棒了 if (wakeupcall.getTimeInMillis()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多