android同时设置多个闹钟

发布于 2024-10-30 13:22:04 字数 3532 浏览 3 评论 0原文

好吧,让我修改一下问题,让它变得更容易。我希望你能为我找到解决方案。两个警报应该同时安排,但这里没有发生这种情况。我什至使用唯一的请求代码来处理待处理的意图。 请帮帮我......

//点击监听器

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);
    }
};

老问题:
我想同时设置两个闹钟以执行两个不同的操作。

对于例如。用户将闹钟设置为 2 点,并将持续时间设置为 15 分钟。第一个警报应在 2 点钟触发,执行功能 1,第二个警报应在 2:15 触发,因为用户指定执行功能 2 的持续时间为 15 分钟。除非用户更改时间,否则应在每天 2 点重复此操作。

我在单击按钮时调用这两个函数:

点击事件

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

setReminder 包含

//设置2点闹钟

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);    
}

wakeReminder包含

//添加持续时间,即15分钟

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);
}

我注意到,每当mAlarmManager.set();成功执行时,LogCat就会显示如下通知
"enqueueToast pkg=com.jellboi.android.togglemode callback=android.app ITransientNotification$stub$proxy@43c0c5f8uration=0"

但是当我同时设置两个闹钟时,通知不会显示在 < code>mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); 执行 但当执行 mAlarmManager.set(AlarmManager.RTC_WAKEUP, milli, pi); 时,会显示通知。此外,通知设置为原始时间,即 2 点钟,而不是在持续时间上添加 15 分钟后。

请帮忙,我尝试了很多方法在不同的地方调用这个函数,比如在第一个警报响起后,但都是徒劳的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

2024-11-06 13:22:04
PendingIntent sender = PendingIntent.getBroadcast(AlarmController.this,0, intent, 0);

你在这一行有问题。如果你想同时触发两个警报那么你应该这样做

PendingIntent sender =       PendingIntent.getBroadcast(AlarmController.this,giveUniqueRequestIdsHere, intent, 0);
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);
宫墨修音 2024-11-06 13:22:04
sender = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), i, PendingIntent.FLAG_ONE_SHOT);

对您的 PendingIntent.getActivity() 执行相同操作;

sender = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), i, PendingIntent.FLAG_ONE_SHOT);

Do the same for your PendingIntent.getActivity();

梦里兽 2024-11-06 13:22:04

您好,尝试使用它来生成请求,我正在生成自己的请求代码,但这条线对我有用

final int intent_id = (int) System.currentTimeMillis();

Hi try using this to generate request I was generating my own request code but this line worked for me

final int intent_id = (int) System.currentTimeMillis();
洒一地阳光 2024-11-06 13:22:04

当启动新的OneShotAlarm.class时,必须设计启动模式。如果第一个实例仍然存在,默认设置将忽略第二次启动。

Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

When launch new OneShotAlarm.class, it have to design launch mode. Default setting would ignore the second time launch, if the first instance still existed.

Intent intent = new Intent(AlarmController.this, OneShotAlarm.class);
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
独闯女儿国 2024-11-06 13:22:04

似乎您忘记在第二次通话之前设置“sender2”。

您更改 sender 的值,然后将 sender2 作为参数。

seems like you forgot to set "sender2" before your second call.

you change the value of sender, then put sender2 as the argument.

烟雨扶苏 2024-11-06 13:22:04

您可以在 Service 类中创建一个任务数组,并在 for 循环中设置该数组,在 for 循环内您可以同时调用您的任务。

You can create an array of your task in the Service class and set the array in the for loop, inside the for loop you can call your task simultaneously at the same time.

糖粟与秋泊 2024-11-06 13:22:04

试试这个:

Calendar cal = new GregorianCalendar();
Date date = cal.getTime();
cal.clear();

Cursor c = database.query(mArray.get(mPosition), new String[]{"days"},"days"+"!="+0, null, null, null, null);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.MONTH,date.getMonth());
cal.set(Calendar.DAY_OF_MONTH,date.getDate());
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(nextScreen.this, AlaramReciver.class);
i.putExtra("Start",mArray.get(mPosition));

Editor medit = getSharedPreferences("Start", 0).edit();
medit.putString("Start", mArray.get(mPosition));medit.commit();

PendingIntent pendingintent = PendingIntent.getBroadcast(this,mPosition, i, 0);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingintent);

你已经设置了这样的时间,一旦你在闹钟管理器中设置了时间,它就会每天自动调用函数。对 pendingintent 使用不同的请求代码。

如果你想调用第二个函数。您只需在 15 分钟内添加时间即可

cal.set(Calender.MINUTE,min+15);

Try this:

Calendar cal = new GregorianCalendar();
Date date = cal.getTime();
cal.clear();

Cursor c = database.query(mArray.get(mPosition), new String[]{"days"},"days"+"!="+0, null, null, null, null);
cal.set(Calendar.YEAR,2011);
cal.set(Calendar.MONTH,date.getMonth());
cal.set(Calendar.DAY_OF_MONTH,date.getDate());
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, min);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(nextScreen.this, AlaramReciver.class);
i.putExtra("Start",mArray.get(mPosition));

Editor medit = getSharedPreferences("Start", 0).edit();
medit.putString("Start", mArray.get(mPosition));medit.commit();

PendingIntent pendingintent = PendingIntent.getBroadcast(this,mPosition, i, 0);
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingintent);

You have set to the time like this, and once you set time in alarm manager it will automatically call the functions daily. Use different request code for pendingintent.

If you want call second function. You just add the time in 15 min in the

cal.set(Calender.MINUTE,min+15);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文