package sdf.d.d;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class AlarmController 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;
private Toast mToast;
private static final String TAG = "app";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.Button00);
button.setOnClickListener(oneShotListener);
button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(startRepeatingListener);
button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(stopRepeatingListener);
/*
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(AlarmController.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(AlarmController.this,AlamrReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(AlarmController.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);
}
},hour,minute,true).show();
}
});
btn_cel.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(AlarmController.this,AlamrReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(AlarmController.this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.cancel(pi);
tv.setText("闹钟取消");
}
});
*/
}
private OnClickListener oneShotListener = new OnClickListener() {
@Override
public void onClick(View v) { //只进行一次定时,不重复执行
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 30); //设置30秒后执行
/*
* 根据PendingIntent.getBroadcast
* 指明定时唤醒广播接收者
* */
PendingIntent pending = PendingIntent.getBroadcast(AlarmController.this,
0, new Intent(AlarmController.this,RepeatingAlarm.class), 0);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending); //表示在指定时间后执行
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(AlarmController.this, "one_shot_scheduled", Toast.LENGTH_LONG).show();
}
};
private OnClickListener startRepeatingListener = new OnClickListener() {
@Override
public void onClick(View v) { //重复执行
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent(AlarmController.this,AlamrReceiver.class);
PendingIntent pend = PendingIntent.getBroadcast(AlarmController.this,
0, intent, 0);
long triggerAtTime = SystemClock.elapsedRealtime();
triggerAtTime +=5*1000; //表示第一次执行15秒后
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerAtTime, 15*1000, pend); //表示过15秒重复唤醒广播接受者
}
};
private OnClickListener stopRepeatingListener = new OnClickListener() {
@Override
public void onClick(View v) { //取消alarm
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pend = PendingIntent.getBroadcast(AlarmController.this,
0, new Intent(AlarmController.this,RepeatingAlarm.class), 0);
am.cancel(pend);
if(mToast!=null){
mToast.cancel();
}
mToast.makeText(AlarmController.this, "stop_repeating_scheduled", Toast.LENGTH_LONG).show();
}
};
}
//==================================================
int triggerAtTime = SystemClock.elapsedRealtime() + 5 * 1000;
❑ ELAPSED_REALTIME_WAKEUP
在指定的演示后,发送广播,并唤醒设备
延时是要把系统启动的时间SystemClock.elapsedRealtime()算进去的,具体用法看代码。