package com.example.test0125;
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;
/**
* Alarm,定时闹钟
* @author ZLQ
*
*/
public class TestAlarm extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//通过 getSystemService取得服务
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar ca = Calendar.getInstance();
ca.set(Calendar.YEAR, 2013);
ca.set(Calendar.MONTH, 0);
ca.set(Calendar.DATE, 25);
ca.set(Calendar.HOUR_OF_DAY, 16);
ca.set(Calendar.MINUTE, 35);
ca.set(Calendar.SECOND, 0);
//取得1970-1-1 0:0 ~目标时间 之间的毫秒数
long time = ca.getTimeInMillis();
// PendingIntent升级版的Intent, 普通Intent是马上执行的
// PendingIntent是在某一个事件触发了之后才去执行的一个Intent
Intent in = new Intent();
in.setClass(this, TestAG.class);
//PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
PendingIntent pen = PendingIntent.getActivity(this, 1, in, 0);
//时间一到就会触发pen,跳转到TestAG.class
am.set(AlarmManager.RTC_WAKEUP, time, pen);
//设置隔过久时间再响
// am.setRepeating(type, triggerAtTime, interval, operation)
//取消响铃
// am.cancel(operation)
}
}