如何在特定时间和日期启动警报,我在哪里使用TimePicker
and 获得时间和日期DatePicker
。即使我可以在用户指定的时间启动警报,但我不能在用户指定的日期这样做。我怎样才能达到我的目标?我已经完成的代码。
package com.example.modifiedalarm;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.Intent;
public class MainActivity extends Activity {
Button b1, b2, b3;
Calendar c;
String abcd;
TextView ed, tv;
int hr, min, year, month, date;
int time = 0;
int daa = 0;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
ed = (TextView) findViewById(R.id.settime);
tv = (TextView) findViewById(R.id.gettime);
c = Calendar.getInstance();
hr = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
date = c.get(Calendar.DAY_OF_MONTH);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_MESSAGE, "New Alarm");
i.putExtra(AlarmClock.EXTRA_HOUR, hr);
i.putExtra(AlarmClock.EXTRA_MINUTES, min);
startActivity(i);
}});
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// showDialog(time);
new TimePickerDialog(MainActivity.this, timeval, hr, min, true)
.show();
}
});
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new DatePickerDialog(MainActivity.this, dateval, year, month,
date).show();
}
});
}
OnDateSetListener dateval = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int Year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
year = Year;
month = monthOfYear;
date = dayOfMonth;
updatedate();
}
};
OnTimeSetListener timeval = new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
hr = hourOfDay;
min = minute;
updatetime();
}
};
public void updatedate() {
tv.setText(new StringBuilder().append(date).append("/").append(month).append("/").append(year));
String og=tv.getText().toString();
//int ogx=Integer.parseInt(og);
Toast.makeText(MainActivity.this, "the date is "+og, Toast.LENGTH_LONG).show();
datecompare();
} private void datecompare() {
}
private void updatetime() {
// TODO Auto-generated method stub
ed.setText(new StringBuilder().append(hr).append(":").append(min));
String b=hr + ":" + min;
}
}
例如:我想在 2013 年 5 月 4 日下午 12:00 设置一个闹钟。我怎样才能做到这一点?