編輯:高級開發
第一步:注冊鬧鐘(AlarmManager,PendingIntent,Intent,鬧鐘的時間)
第二步:注冊鬧鐘廣播接受者(BroadcastReceiver)
第三步:處理鬧鐘的時間(比如:打開一個應用,提示用戶時間到了 etc....)
=========AlarmAlert.Java==========
private void updateLayout() {
。。。。。。。。。。
。。。。。。。。。。
/* snooze behavior: pop a snooze confirmation vIEw, kick alarm
manager. */
Button snooze = (Button) findVIEwById(R.id.snooze);
snooze.requestFocus();
snooze.setOnClickListener(new Button.OnClickListener() {
public void onClick(VIEw v) {
snooze();
}
});
........
........
}
// Attempt to snooze this alert.
private void snooze() {
final String snooze =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(SettingsActivity.KEY_ALARM_SNOOZE, DEFAULT_SNOOZE);
int snoozeMinutes = Integer.parseInt(snooze);
final long snoozeTime = System.currentTimeMillis()
+ (1000 * 60 * snoozeMinutes); //計算睡眠時間
Alarms.saveSnoozeAlert(AlarmAlert.this, mAlarm.id, snoozeTime);//保存睡眠的時間
// Get the display time for the snooze and update the notification.
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(snoozeTime);
。。。。
。。。。
。。。。
// Notify the user that the alarm has been snoozed.//通知給廣播接收者 AlarmReceiver.class 並處理鬧鐘時間
Intent cancelSnooze = new Intent(this, AlarmReceiver.class);
cancelSnooze.setAction(Alarms.CANCEL_SNOOZE);
cancelSnooze.putExtra(Alarms.ALARM_ID, mAlarm.id);
PendingIntent broadcast =
PendingIntent.getBroadcast(this, mAlarm.id, cancelSnooze, 0);
接上頁
NotificationManager nm = getNotificationManager();
。。。。。
。。。。。。。。。。。。。
stopService(new Intent(Alarms.ALARM_ALERT_ACTION));
finish();
}
=========Alarm.Java==========
static void saveSnoozeAlert(final Context context, final int id,
final long time) {
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
SharedPreferences.Editor ed = prefs.edit();
if (id == -1) {
clearSnoozePreference(ed);
} else {
ed.putInt(PREF_SNOOZE_ID, id);
ed.putLong(PREF_SNOOZE_TIME, time); //保存提示的時間
ed.commit();
}
// Set the next alert after updating the snooze.
setNextAlert(context);
}
/**
* If there is a snooze set, enable it in AlarmManager
* @return true if snooze is set
*/
private static boolean enableSnoozeAlert(final Context context) {
SharedPreferences prefs = context.getSharedPreferences(
AlarmClock.PREFERENCES, 0);
int id = prefs.getInt(PREF_SNOOZE_ID, -1);
if (id == -1) {
return false;
}
long time = prefs.getLong(PREF_SNOOZE_TIME, -1); //取出設置鬧鐘的時間
// Get the alarm from the db.
final Alarm alarm = getAlarm(context.getContentResolver(), id);
// The time in the database is either 0 (repeating) or a specific time
// for a non-repeating alarm. Update this value so the AlarmReceiver
// has the right time to compare.
alarm.time = time; //將時間放入數據庫中
enableAlert(context, alarm, time);
return true;
}
/**
* Sets alert in AlarmManger and StatusBar. This is what will
* actually launch the alert when the alarm triggers.
接上頁
*
* @param alarm Alarm.
* @param atTimeInMillis milliseconds since epoch
*/
private static void enableAlert(Context context, final Alarm alarm,
final long atTimeInMillis) {
AlarmManager am = (AlarmManager)
context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_ALERT_ACTION);
PendingIntent sender = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); //接受廣播的鬧鐘的廣播意圖 Action:“ALARM_ALERT_ACTION”
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender); //設置鬧鐘
/**
//鬧鐘的廣播接受者
* Glue class: connects AlarmAlert IntentReceiver to AlarmAlert
* activity. Passes through Alarm ID.
*/
public class AlarmReceiver extends BroadcastReceiver {
.......
.....
.......
/* launch UI, explicitly stating that this is not due to user action
* so that the current app's notification management is not disturbed */
Intent alarmAlert = new Intent(context, c);
alarmAlert.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alarmAlert);
...
........
// Play the alarm alert and vibrate the device.
Intent playAlarm = new Intent(Alarms.ALARM_ALERT_ACTION);
playAlarm.putExtra(Alarms.ALARM_INTENT_EXTRA, alarm);
context.startService(playAlarm);
...........
.....
}
當我們創建一個帶EditText 或 AutoCompleteTextVIEw的視圖時,在加載視圖時總是會把輸入的焦點自動移動到第一個輸入框。如下圖所示:
之前我們曾向您介紹過在android中實現service動態更新UI界面,在UI設計中需要利用很多圖庫相冊軟件,而Gallery 是國外一個免費開源的、功能非常強大、有
在後台線程中執行各種操作(網絡連接、大數據存儲)的時候,我們希望讓客戶能看到後台有操作在進行,那麼既能有效的提示用戶,又不占用當前操作空間,最好的方法就是在標題欄有個進
Google於2007年11月5日宣布發布android 手機,這款手機是基於Linux內核的一款操作系統,谷歌將繼續努力,讓這些服務變得更好,同時也將添加更有吸引力的