Android自動(dòng)開關(guān)機(jī)實(shí)現(xiàn)詳細(xì)教程
1. 簡介
我的實(shí)現(xiàn)是在設(shè)置程序里面增加一個(gè)接口,讓用戶設(shè)置自動(dòng)開關(guān)機(jī),這個(gè)自動(dòng)開關(guān)機(jī)的設(shè)置可以參照鬧鐘的設(shè)置。關(guān)于自動(dòng)關(guān)機(jī),考慮到關(guān)機(jī)的時(shí)候,用戶可能正有一些重要的操作,那么應(yīng)該給用戶一個(gè)機(jī)會(huì)去取消當(dāng)前的關(guān)機(jī)。
1)一個(gè)BroadcastReceiver, 接收如下信息:
a) 自定義的ACTION_REQUEST_POWER_OFF:設(shè)置auto power off時(shí),通過AlarmManager設(shè)置的一個(gè)RTC_WAKEUP時(shí)鐘。當(dāng)?shù)皆O(shè)置的關(guān)機(jī)時(shí)間時(shí),之前設(shè)置到AlarmManager的這個(gè)action會(huì)被廣播。我們實(shí)現(xiàn)的這個(gè)BroadcastReceiver接收到這個(gè)消息后,就要開始power off流程
b)
c) BOOT_COMPLETE和TIMEZONE changed, Time set等時(shí)間相關(guān)的action:當(dāng)系統(tǒng)開機(jī)完成或時(shí)間、時(shí)區(qū)發(fā)生改變時(shí),都需要重新設(shè)置alarm。
2)一個(gè)處理power off 的Service,當(dāng)BroadcastReceiver接收到ACTION_REQUEST_POWER_OFF,我們給用戶一個(gè)機(jī)會(huì)去取消當(dāng)前的自動(dòng)關(guān)機(jī)。這個(gè)Service的作用就是啟動(dòng)一個(gè)無背景的頁面,給用戶提示。同時(shí)播放之前用戶設(shè)置的提示音或振動(dòng)。
3)一個(gè)Activity:顯示一個(gè)dialog提示用戶要自動(dòng)關(guān)機(jī),并用一個(gè)計(jì)時(shí)器倒計(jì)時(shí)。當(dāng)用戶確認(rèn)關(guān)機(jī),或者計(jì)時(shí)器到時(shí)間的時(shí)候,就關(guān)機(jī)。否則取消當(dāng)前關(guān)機(jī),并重設(shè)下次自動(dòng)關(guān)機(jī)alarm。
2. 自動(dòng)關(guān)機(jī)的實(shí)現(xiàn)
自動(dòng)關(guān)機(jī)的實(shí)現(xiàn)比較簡單,這里主要說一下怎么設(shè)置alarm,和實(shí)現(xiàn)關(guān)機(jī):
1)設(shè)置自動(dòng)關(guān)機(jī)的alarm:
- AlarmManager am = (AlarmManager) context
- .getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(
- "com.android.settings.action.REQUEST_POWER_OFF");
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
- intent, PendingIntent.FLAG_CANCEL_CURRENT);
- am = (AlarmManager) context
- .getSystemService(Context.ALARM_SERVICE);
- am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
2)自動(dòng)關(guān)機(jī)調(diào)的是:
./frameworks/base/services/java/com/android/server/ShutdownActivity.java
- Intent newIntent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
- newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(newIntent);
Intent.ACTION_REQUEST_SHUTDOWN是Intent里面一個(gè)隱藏的action。
3. 自動(dòng)開機(jī)的實(shí)現(xiàn)
一直在做上層應(yīng)用和framework,對于底層不是很熟悉。正好有同事之前做過關(guān)機(jī)鬧鈴,所以把他之前的實(shí)現(xiàn)稍加改動(dòng)就可以了。在系統(tǒng)power off的狀態(tài)下自動(dòng)開機(jī),我們需要設(shè)置一個(gè)rtc時(shí)鐘,當(dāng)用戶設(shè)置自動(dòng)開機(jī)時(shí),由AlarmManagerService將時(shí)鐘設(shè)置下去。這學(xué)要底層的支持。這里的實(shí)現(xiàn)是定義一個(gè)我們自己的rtc alarm type:
1) 首先要在頭文件里面定義:
- a) kernel/include/linux/android_alarm.h
- #define ANDROID_ALARM_GET_TIME(type) ALARM_IOW(4, type, struct timespec)
- #define ANDROID_ALARM_SET_RTC _IOW('a', 5, struct timespec)
- #define ANDROID_RTC_ALARM_SET _IOW('a', 7, int)
- #define ANDROID_ALARM_BASE_CMD(cmd) (cmd & ~(_IOC(0, 0, 0xf0, 0)))
- b) bionic/libc/kernel/common/linux/android_alarm.h
- #define ANDROID_RTC_ALARM_SET _IOW('a', 7, int)
2)
- case ANDROID_RTC_ALARM_SET:
- {
- unsigned int rtc_alarm_time;
- struct rtc_time rtc_now;
- if (copy_from_user(&rtc_alarm_time, (void __user *)arg,
- sizeof(rtc_alarm_time))) {
- rv = -EFAULT;
- goto err1;
- }
- if (pmic_rtc_get_time(&rtc_now) < 0) {
- rtc_now.sec = 0;
- if (pmic_rtc_start(&rtc_now) < 0) {
- printk("get and set rtc info failed\n");
- break;
- }
- }
- pmic_rtc_disable_alarm(PM_RTC_ALARM_1);
- rtc_now.sec += rtc_alarm_time;
- pmic_rtc_enable_alarm(PM_RTC_ALARM_1, &rtc_now);
- break;
- }
當(dāng)然不要忘記增加一個(gè)include:
#include
3)在frameworks/base/services/jni/com_android_server_AlarmManagerService.cpp里面增加一個(gè)方法去設(shè)置時(shí)鐘:
- static void android_server_AlarmManagerService_updateRtcAlarm(JNIEnv* env, jobject obj, jint fd, jint seconds)
- {
- #if HAVE_ANDROID_OS
- int result = ioctl(fd, ANDROID_RTC_ALARM_SET, &seconds);
- LOGE("set rtc alarm to %d later: %s\n", seconds, strerror(errno));
- if (result < 0)
- {
- LOGE("Unable to set rtc alarm to %d later: %s\n", seconds, strerror(errno));
- }
- #endif
- }
還有就是不要忘記定義一下接口:
{"updateRtcAlarm", "(II)V", (void*)android_server_AlarmManagerService_updateRtcAlarm},
4)
定義:private native void updateRtcAlarm(int fd, int seconds);
調(diào)用:
- public void setRepeating(int type, long triggerAtTime, long interval,
- PendingIntent operation) {
- if (operation == null) {
- Slog.w(TAG, "set/setRepeating ignored because there is no intent");
- return;
- }
- synchronized (mLock) {
- Alarm alarm = new Alarm();
- alarm.type = type;
- alarm.when = triggerAtTime;
- alarm.repeatInterval = interval;
- alarm.operation = operation;
- // Remove this alarm if already scheduled.
- removeLocked(operation);
- if (localLOGV) Slog.v(TAG, "set: " + alarm);
- int index = addAlarmLocked(alarm);
- if (index == 0) {
- setLocked(alarm);
- }
- // Start to setup auto power on alarm
- if ((alarm.type == AlarmManager.ELAPSED_REALTIME_WAKEUP) &&
- alarm.operation.getTargetPackage().equals("com.android.settings")) {
- updateRtcAlarm(mDescriptor, (int)((alarm.when - System.currentTimeMillis()) / 1000));
- }
- // End to setup auto power on alarm
- }
- }
5)在應(yīng)用層設(shè)置自動(dòng)開機(jī):
- AlarmManager am = (AlarmManager) context
- .getSystemService(Context.ALARM_SERVICE);
- Intent intent = new Intent(
- "com.android.settings.action.REQUEST_POWER_ON");
- PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
- intent, PendingIntent.FLAG_CANCEL_CURRENT);
- am = (AlarmManager) context
- .getSystemService(Context.ALARM_SERVICE);
- am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pendingIntent);
4. 總結(jié)
1)自動(dòng)開機(jī)原理比較簡單,但是需要底層的支持,所以對于做應(yīng)用或者framework層的技術(shù)人員來說,實(shí)現(xiàn)起來稍微比較麻煩。
2) 在設(shè)置自動(dòng)開關(guān)機(jī)的時(shí)候,需要考慮的情況很多,比如是否設(shè)置時(shí)間/時(shí)區(qū)的改變,手機(jī)當(dāng)前是開機(jī)還是關(guān)機(jī)狀態(tài)等。