Android的报警设定具体日期具体、日期、Android

2023-09-13 02:12:59 作者:伶仃

我婉与通知在特定的日期设置闹钟。 然后,我使用AmarmManager与NotificationManager目前。 当我设置的dateDialog选定的日期,报警工作。 我怎样才能把日历值报警设定固定的时间? 我想每天在固定的时间在早上9:00,如重复报警。 目前,报警被忽略了时间上的具体日期。 你可以帮帮我吗?非常感谢。

  confirmButton.setOnClickListener(新View.OnClickListener()
    {
        公共无效的onClick(视图v){
            //与到期日设定报警
            AM =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
            setOneTimeAlarm();
            Toast.makeText(fridgeDetails.this,报警自动设置,Toast.LENGTH_SHORT).show();
            的setResult(RESULT_OK);
            完();
        }
        公共无效setOneTimeAlarm(){
        c.set(Calendar.HOUR_OF_DAY,10);
        c.set(Calendar.MINUTE,0);
        c.set(expiredYear,expiredMonth,expiredDay);
        意图myIntent =新的意图(fridgeDetails.this,AlarmService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this,
                        0,myIntent,PendingIntent.FLAG_ONE_SHOT);
        am.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

        }
    });


私人DatePickerDialog.OnDateSetListener mDateSetListener =新DatePickerDialog.OnDateSetListener()
{
    公共无效onDateSet(DatePicker的观点,年整型,诠释monthOfYear,
            INT DAYOFMONTH){
        expiredYear =年;
        expiredMonth = monthOfYear;
        expiredDay = DAYOFMONTH;
        displayDate();
    }
};

公共类AlarmService扩展的BroadcastReceiver {
NotificationManager处;
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
    纳米=(NotificationManager)上下文
            .getSystemService(Context.NOTIFICATION_SERVICE);
          从CharSequence的=检查你的冰箱;
          CharSequence的消息=是时候吃的!
          PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
            新意图(),0);
          注意notif =新的通知(R.drawable.ic_launcher,
            保持冰箱,System.currentTimeMillis的());
          notif.setLatestEventInfo(上下文,从,消息,contentIntent);
          nm.notify(1,notif);
         }
}
 

解决方案

您应该叫公共无效setRepeating(int型的,长triggerAtTime,间隔长,PendingIntent操作)( 看到这里)重复报警。例如,你想天天火灾报警的上午9点,你可以这样做:

 日历C = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY,9);
c.set(Calendar.MINUTE,0);
setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
 
怎样把文件属性里的日期和修改时间改变

此外,initilazing的PendingIntent,当最后一个参数设置为0。

  PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this,
                        0,myIntent,0);
 

I wan to set alarm with notification at specific date. Then I am using AmarmManager with NotificationManager currently. When I set selected date from dateDialog, the alarm is working. How can I put calendar value on alarm set with fixed time? I want to repeat alarm every day on fixed time such as at 9:00 in the morning. Currently, alarm is ignoring the time on specific date. Could you help me? Many thanks.

confirmButton.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v) {
            //set alarm with expiration date                
            am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            setOneTimeAlarm();
            Toast.makeText(fridgeDetails.this, "Alarm automatic set", Toast.LENGTH_SHORT).show();
            setResult(RESULT_OK);
            finish();
        }
        public void setOneTimeAlarm() {
        c.set(Calendar.HOUR_OF_DAY, 10);
        c.set(Calendar.MINUTE, 0);
        c.set(expiredYear, expiredMonth, expiredDay);
        Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, PendingIntent.FLAG_ONE_SHOT);
        am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);

        }
    });


private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() 
{
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        expiredYear = year;
        expiredMonth = monthOfYear;
        expiredDay = dayOfMonth;
        displayDate();
    }
};  

public class AlarmService extends BroadcastReceiver{
NotificationManager nm;
@Override
public void onReceive(Context context, Intent intent) {
    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
          CharSequence from = "Check your fridge";
          CharSequence message = "It's time to eat!";
          PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(), 0);
          Notification notif = new Notification(R.drawable.ic_launcher,
            "Keep Fridge", System.currentTimeMillis());
          notif.setLatestEventInfo(context, from, message, contentIntent);
          nm.notify(1, notif);
         }  
}

解决方案

You should call public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)(see here) to repeat the alarm. For example, you want to fire the alarm on 9:00 am every day, you can do :

Calendar c=Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, 9);
c.set(Calendar.MINUTE, 0);
setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);

Also,set the last parameter to 0 when initilazing the PendingIntent.

PendingIntent pendingIntent = PendingIntent.getBroadcast(fridgeDetails.this, 
                        0, myIntent, 0);