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)
}
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11