public class Test extends Activity { /** Called when the activity is first created. */ private TextView tv = null; private Button btn_set = null; private Button btn_cel = null; private Calendar c = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) this.findViewById(R.id.TextView); btn_set = (Button) this.findViewById(R.id.Button01); btn_cel = (Button) this.findViewById(R.id.Button02); c = Calendar.getInstance(); btn_set.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); new TimePickerDialog(Test.this,new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub c.setTimeInMillis(System.currentTimeMillis()); c.set(Calendar.HOUR_OF_DAY, hourOfDay); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); Intent intent = new Intent(Test.this,AlamrReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(Test.this, 0, intent, 0); AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);//设置闹钟 am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (10*1000), pi);//重复设置 tv.setText("设置的闹钟时间为:"+hourOfDay+":"+minute); }