Android的设置多个闹钟simultaneosuly [英] android set multiple alarms simultaneosuly

查看:222
本文介绍了Android的设置多个闹钟simultaneosuly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,让我修改的问题,使之更容易。我希望你能够让我这样做的解决方案。两个报警器应同步调度,这是不会发生在这里。我甚至用独特的请求,$ C $下挂起的意图。 请帮我.....

  

//在点击监听器

 私人OnClickListener mOneShotListener =新OnClickListener(){
    公共无效的onClick(视图v){

        意向意图=新的意图(AlarmController.this,OneShotAlarm.class);
        PendingIntent发送= PendingIntent.getBroadcast(AlarmController.this,0,意向,0);
        //我甚至尝试发送= PendingIntent.getBroadcast(AlarmController.this,0,意向,PendinIntent.FLAG_UPDATE_CURRENT);


        //警报从现在熄灭30秒。

        台历挂历= Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis的());
        calendar.add(Calendar.SECOND,30);

        //调度报警!

        AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),发送方);

        //增加了2分钟的时间

        calendar.add(Calendar.MINUTE,2);

        发件人= PendingIntent.getBroadcast(AlarmController.this,1,意向,0);
        am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),sender2);
    }
};
 

老问题:
我想设置两个闹钟,在所要执行两种不同的操作同时进行。

有关例如。用户设置闹钟,以在2'O时钟,设置时间限制在15分钟。第一个报警器应开火2'O时钟,执行功能1,第二报警器应开火2:15为用户指定的时间为15分钟,其中执行函数2。这个操作应该每天重复在2'O时钟,除非用户更改的时间。

我打电话这两个功能按钮点击:

  

在Click事件

  saveButton.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图查看){
        ..........
        ..........
        新ReminderManager(本).setReminder(mRowId,mCalendar);
        新ReminderManager(本).wakeReminder(mRowId,mCalendar,持续时间);
        }
    }
 

该setReminder包含

  

//内置2'O时钟报警

 公共无效setReminder(长TASKID,日历时,串持续时间){
    意图I =新的意图(mContext,OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID,(长)TASKID);
    PendingIntent PI = PendingIntent.getBroadcast(mContext,(INT)System.currentTimeMillis的(),我,PendingIntent.FLAG_UPDATE_CURRENT);
    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,when.getTimeInMillis(),AlarmManager.INTERVAL_DAY,PI);
}
 

该wakeReminder包含

  

//添加时间,即15分钟

 公共无效wakeReminder(长TASKID,日历时,串持续时间){
    意图I =新的意图(mContext,OnAlarmReceiverWake.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID,(长)TASKID);
        龙D =的Long.parseLong(持续时间);
        龙分钟= D * 60 * 100;
        长毫= when.getTimeInMillis()+分钟;
        PendingIntent PI = PendingIntent.getBroadcast(mContext,(INT)System.currentTimeMillis的(),我,PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.set(AlarmManager.RTC_WAKEUP,毫,PI);
}
 

我注意到,每当 mAlarmManager.set(); 成功执行 LogCat中显示通知,如
enqueueToast PKG = com.jellboi.android.togglemode回调= android.app ITransientNotification $存根$ @代理期限43c0c5f8 = 0

但是当我同时设置两个报警通知不显示时, mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,when.getTimeInMillis(),AlarmManager.INTERVAL_DAY,PI); 执行 执行;但是,当 mAlarmManager.set(AlarmManager.RTC_WAKEUP,毫,PI)通知显示。还通知被设置为原始时间即2'O时钟而不是之后加入15分钟,以它的持续时间。

请帮忙,我想了很多办法,在不同的地方,如1号报警被触发后,调用此函数,但一切都是徒劳的。

解决方案

  PendingIntent发送= PendingIntent.getBroadcast(AlarmController.this,0,意向,0);
 

您在这条线有问题。如果你想同时令两部alarma那么你应该做这样的事

  PendingIntent发送= PendingIntent.getBroadcast(AlarmController.this,giveUniqueRequestIdsHere,意向,0);
 

Ok, let me modify the question and make it much easier. I Hope you'll be able to get me the solution for this. Both the alarms should be scheduled simultaneously, which is not happening here. I am even using unique requestcode for the pending intent. HELP ME please.....

//On click Listener

private OnClickListener mOneShotListener = new OnClickListener() {
    public void onClick(View v) {

        Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
        PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);
        //I even tried sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, PendinIntent.FLAG_UPDATE_CURRENT);


        //the alarm to go off 30 seconds from now.

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 30);

        // Schedule the alarm!

        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);

        //adds 2 minutes to the time

        calendar.add(Calendar.MINUTE, 2);

        sender = PendingIntent.getBroadcast(AlarmController.this,1, intent,0);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender2);
    }
};

OLD Question:
I want to set two alarms at the same time for two different operations to be performed.

For Eg. A user sets alarm at 2'O clock and sets the duration to 15 mins. The first alarm should fire at 2'O clock which performs function1 and the second alarm should fire at 2:15 as the user specified the duration as 15 mins which performs the function2. This operation should be repeated everyday at 2'O clock unless the user changes the time.

I am calling this both functions on button click:

On Click Event

saveButton.setOnClickListener(new View.OnClickListener() {  
    public void onClick(View view) {  
        ..........  
        ..........  
        new ReminderManager(this).setReminder(mRowId, mCalendar);  
        new ReminderManager(this).wakeReminder(mRowId, mCalendar, duration);  
        }  
    }

The setReminder contains

//sets alarm at 2'O clock

public void setReminder(Long taskId, Calendar when, String duration){  
    Intent i = new Intent(mContext, OnAlarmReceiver.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);    
    PendingIntent pi = PendingIntent.getBroadcast(mContext,(int)System.currentTimeMillis(), i, PendingIntent.FLAG_UPDATE_CURRENT);    
    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);    
}

The wakeReminder contains

//adds duration i.e. 15mins

public void wakeReminder(Long taskId, Calendar when, String duration){
    Intent i = new Intent(mContext, OnAlarmReceiverWake.class);
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);
        Long d = Long.parseLong(duration);
        Long mins = d*60*100; 
        Long milli = when.getTimeInMillis() + mins;
        PendingIntent  pi = PendingIntent.getBroadcast(mContext, (int)System.currentTimeMillis(), i, PendingIntent.FLAG_UPDATE_CURRENT);
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi);
}

I have noticed that whenever mAlarmManager.set(); is executed successfully the LogCat shows notification like
"enqueueToast pkg=com.jellboi.android.togglemode callback=android.app ITransientNotification$stub$proxy@43c0c5f8 duration=0"

but when I set both the alarms simultaneously the notification is not shown when the mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); is executed but the notification is shown when mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi); is executed. Also the notification is set for the original time i.e. 2'O clock and not after adding 15mins to it's duration.

Please help, I tried a lot of ways to call this functions at different places like after the 1st alarm is fired but all in vain.

解决方案

PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);

You have problem in this line. If you want to simultaneously fire two alarma then you should do something like this

PendingIntent sender =       PendingIntent.getBroadcast(AlarmController.this,giveUniqueRequestIdsHere, intent, 0);

这篇关于Android的设置多个闹钟simultaneosuly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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