我正在Android 2.1上开发一个任务管理器。我想为datepicker中的日期设置任务设置警报和时间选择器的时间帮助我使用代码..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText next = (EditText) findViewById(R.id.editText1);
final Button sub = (Button) findViewById(R.id.button1);
final Button res = (Button) findViewById(R.id.button2);
final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1);
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
int y=dp.getYear();
int mo=dp.getMonth();
int day=dp.getDayOfMonth();
Time t = new Time();
int h=t.getCurrentHour();
int m=t.getCurrentMinutes();
}
private AlarmManager getSystemService(String alarmService) {
// TODO Auto-generated method stub
return null;
}
});
答案 0 :(得分:7)
嘿,这是如何将android AlarmManager上的闹钟设置为特定日期 (android alarmmanager在日期设置闹钟) 我一直在寻找这个。 注意月份的价值!!
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//cal.add(Calendar.SECOND, 10);
cal.set(Calendar.DATE,19); //1-31
cal.set(Calendar.MONTH,Calendar.DECEMBER); //first month is 0!!! January is zero!!!
cal.set(Calendar.YEAR,2012);//year...
cal.set(Calendar.HOUR_OF_DAY, 16); //HOUR
cal.set(Calendar.MINUTE, 39); //MIN
cal.set(Calendar.SECOND, 10); //SEC
// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, alarmAct.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);
//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
答案 1 :(得分:2)
这是设置闹钟的方法。
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
//when does this go off?
Long mHowLongFromNowInMilliseconds = 10000 //(10 seconds from now)
calendar.add(Calendar.MILLISECOND, mHowLongFromNowInMilliseconds);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
日历不是必需的...但它可能会有所帮助;)
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent);
!!!请记住:当设备完全断电时,警报会被取消。
http://developer.android.com/reference/android/app/AlarmManager.html
答案 2 :(得分:1)
Android不会为您播放闹钟,Alarm Manager允许您安排应用程序在将来的某个时间点运行。 因此,只需在AlarmManager中添加时间和待处理的意图,并在调用此意图时播放音乐。
访问:http://developer.android.com/reference/android/app/AlarmManager.html
答案 3 :(得分:0)
查看此link以获取完整示例。
答案 4 :(得分:0)
public class AlarmService extends Activity {
private PendingIntent mAlarmSender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an IntentSender that will launch our service, to be scheduled
// with the alarm manager.
mAlarmSender = PendingIntent.getService(AlarmService.this,
0, new Intent(AlarmService.this, AlarmService_Service.class), 0);
setContentView(R.layout.alarm_service);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.start_alarm);
button.setOnClickListener(mStartAlarmListener);
button = (Button)findViewById(R.id.stop_alarm);
button.setOnClickListener(mStopAlarmListener);
}
private OnClickListener mStartAlarmListener = new OnClickListener() {
public void onClick(View v) {
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 30*1000, mAlarmSender);
// Tell the user about what we did.
Toast.makeText(AlarmService.this, R.string.repeating_scheduled,
Toast.LENGTH_LONG).show();
}
};
private OnClickListener mStopAlarmListener = new OnClickListener() {
public void onClick(View v) {
// And cancel the alarm.
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(mAlarmSender);
// Tell the user about what we did.
Toast.makeText(AlarmService.this, R.string.repeating_unscheduled,
Toast.LENGTH_LONG).show();
}
};
}