在特定时间设置android闹钟 [英] set android alarm at specific time

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

问题描述

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

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

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

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


解决方案

好的,你需要设置闹钟来响铃下一次是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));

日期dat = new Date(); //初始化到现在
日历cal_alarm = Calendar.getInstance();
日历cal_now = Calendar.getInstance();
cal_now.setTime(dat);
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY,5); //设置闹钟时间
cal_alarm.set(Calendar.MINUTE,59);
cal_alarm.set(Calendar.SECOND,0);
if(cal_alarm.before(cal_now)){//如果它在过去增加
cal_alarm.add(Calendar.DATE,1);
}
//设置您的AlarmManager在这里

}

我想给你一个可建立的例子,但是我并不完全了解闹钟,所以这就是你所拥有的。建立在日食3.5.2与ADK 15


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

解决方案

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

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

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