与Android的通知报警运行的任何日期当我改变日期 [英] Android alarm with notification running any date when I change date

查看:365
本文介绍了与Android的通知报警运行的任何日期当我改变日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AlarmManager NotificationManager 广播接收器
当我设置的报警与特定的日期,它是工作的第一次。然而,当我修改的日期和点击确认按钮,报警器工作正常的任何日期
立即。我想截止日期以固定的时间后,设置报警时间间隔一天。
什么是它存在的问题?我不此刻明白了。

  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,14);
    c.set(Calendar.MINUTE,49);
    c.set(expiredYear,expiredMonth,expiredDay);
    意图myIntent =新意图(fridgeDetails.this,AlarmService.class);
    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(
        fridgeDetails.this,0,myIntent,PendingIntent.FLAG_ONE_SHOT);
    am.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY,的PendingIntent);
}
});

AlarmService.java

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


解决方案

您需要设置属性而不是FLAG_ONE_SHOT。这是不是重复单一的报警事件。试试这个

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

看到关于更详细的这里

编辑:

当你与通知的PendingIntent做

 的PendingIntent contentIntent = PendingIntent.getActivity(背景下,0,新意图(),0);

在此传递意图的空对象,你需要通过类名,当你点击该通知,活动将推出像闹钟设定的时间你做

 意图myIntent =新意图(fridgeDetails.this,AlarmService.class);
的PendingIntent的PendingIntent = PendingIntent.getBroadcast(
        fridgeDetails.this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

现在只是在意向传递活性的研究名称,比如假设你想启动您的家庭活动,活动名称,如homeactivity

 的PendingIntent contentIntent = PendingIntent.getActivity(背景下,0,新的意图(背景下,HomeActivity.class),0);

I am using AlarmManager and NotificationManager with BroadcastReceiver. When I set an alarm with a specific date, it is working the first time. However, when I modify the date and click the confirm button, the alarm is working any date immediately. I want to set the alarm interval day after expired date with fixed time. What is the problem with it? I don't understand at the moment.

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, 14);
    c.set(Calendar.MINUTE, 49);
    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.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, pendingIntent);
}
}); 

AlarmService.java

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);
        notif.defaults |= Notification.DEFAULT_SOUND; 
        notif.flags |= Notification.FLAG_AUTO_CANCEL; 
        nm.notify(1, notif);
    }   
}

解决方案

You need to set the property instead of FLAG_ONE_SHOT. This is for single alarm event not for the repeat. try this

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

see more detail about from here

Edit:

As you do for notification with PendingIntent

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);

In this you pass the empty object of Intent you need to pass the class name for that when you click on notification which Activity will be launch like in Alarm set time you do

Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
        fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

now just pass the Acitivity name in the intent like suppose you want to launch your home activity and activity name like "homeactivity"

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0);

这篇关于与Android的通知报警运行的任何日期当我改变日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆