在特定时间设置 Android 闹钟

发布于 2024-12-15 20:47:14 字数 496 浏览 2 评论 0原文

我在特定时间设置了闹钟,但每次我打开应用程序时它都会打开 这是我使用的代码:

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0010000,intent,0);

Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 5);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);

i set the alarm at specific time but every time i open the application it will turn on
this is the code i used :

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0010000,intent,0);

Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR, 5);
time.set(Calendar.MINUTE, 59);
time.set(Calendar.SECOND, 0);

alarmManager.set(AlarmManager.RTC,time.getTimeInMillis(),pendingIntent);

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

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

发布评论

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

评论(2

她比我温柔 2024-12-22 20:47:14

好的,您需要将闹钟设置为下次响铃时间为 5:59:00。
您可以通过获取当前时间来完成此操作,如果在 5:59:00 之前,则设置闹钟,如果在 5:59:00 之后,则添加一天并设置闹钟。这样做:

import java.util.Calendar;
import java.util.Date;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Panel(this));

    Date dat  = new Date();//initializes to now
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);
    cal_alarm.setTime(dat);
    cal_alarm.set(Calendar.HOUR_OF_DAY,5);//set the alarm time
    cal_alarm.set(Calendar.MINUTE, 59);
    cal_alarm.set(Calendar.SECOND,0);
    if(cal_alarm.before(cal_now)){//if its in the past increment
        cal_alarm.add(Calendar.DATE,1);
    }
    //SET YOUR AlarmManager here

}

我想给你一个可构建的例子,但我还没有完全理解alarmmanager,所以这就是你所拥有的。基于 eclipse 3.5.2 和 ADK 15 构建

Alright, you need to set the alarm to ring the next time it is 5:59:00.
You do this by getting the current time, if its before 5:59:00, set the alarm, if its after 5:59:00 then add a day and set the alarm. Do it like so:

import java.util.Calendar;
import java.util.Date;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new Panel(this));

    Date dat  = new Date();//initializes to now
    Calendar cal_alarm = Calendar.getInstance();
    Calendar cal_now = Calendar.getInstance();
    cal_now.setTime(dat);
    cal_alarm.setTime(dat);
    cal_alarm.set(Calendar.HOUR_OF_DAY,5);//set the alarm time
    cal_alarm.set(Calendar.MINUTE, 59);
    cal_alarm.set(Calendar.SECOND,0);
    if(cal_alarm.before(cal_now)){//if its in the past increment
        cal_alarm.add(Calendar.DATE,1);
    }
    //SET YOUR AlarmManager here

}

I wanted to give you a buildable example, but i don't fully understand alarmmanager yet, so this is what you have. Built on eclipse 3.5.2 with ADK 15

岁月打碎记忆 2024-12-22 20:47:14

不要将此代码放置在 onCreate 函数中,而是将其放置在任何按钮事件或仅在用户交互时调用的任何其他事件中。
我想,这样就可以了。

PendingIntent pendingIntent =  PendingIntent.getBroadcast(this,0010000,intent,0);
pendingIntent.cancel();

将此“取消”方法添加到您的代码中。这将在设置新警报之前取消上一个警报。

Instead of placing this code in onCreate function, place it inside any button event or any other event which will be called only on user interaction.
I think, that'll do.

PendingIntent pendingIntent =  PendingIntent.getBroadcast(this,0010000,intent,0);
pendingIntent.cancel();

add this 'cancel' method to your code. this will cancel the previous alarm before setting the new one.

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