本文介绍了Android - 如何将闹钟设置为特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看过很多教程,现在已经尝试了 2 个小时,但仍然有问题.我现在很紧张 :) 我想设置闹钟,例如到16:25出发,但什么也没发生.我有这个代码:
I have seen a lot of tutorials and been trying for 2 hours now , though something is still wrong. I am very nervous now :) I want to set an alarm e.g. to 16:25 to go off, but nothing happens. I have this code:
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 25);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
我也试过这个:
cal.set(Calendar.AM_PM, cur_cal.get(Calendar.AM_PM));
cal.set(Calendar.HOUR, 4);
我的最终目标是让它成为一个重复的闹钟,例如它应该每天在设定的时间关闭.
My final goal is to make it a repeating alarm, e.g. it should go off every day at the set time.
2011 年 1 月 17 日更新.还是行不通.我有这个代码:
Update 01.17.2011. Still not working. I have this code:
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2011);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 17);
cal.set(Calendar.HOUR_OF_DAY, 16);
cal.set(Calendar.MINUTE, 58);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
我也试过这个:
cal.set(Calendar.HOUR, 4);
cal.set(Calendar.AM_PM, Calendar.PM);
还有这个:
cal.set(Calendar.HOUR_OF_DAY, 4 );
cal.set(Calendar.AM_PM, Calendar.PM);
推荐答案
package your.pack.name;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2012,2,8,18,16);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// cal.add(Calendar.SECOND, 5);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
}
这篇关于Android - 如何将闹钟设置为特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!