My implementation is in the Setup program to add an interface to allow users to set the automatic switching machine, this automatic switching machine settings can refer to the setting of the alarm clock. About the automatic shutdown, considering the shutdown, the user may be some important operations, then should give the user a chance to cancel the current shutdown.
1 a broadcastreceiver, receive the following information:
A custom Action_request_power_off: sets a rtc_wakeup clock that is set by Alarmmanager when Auto Power is off. This action, which was previously set to Alarmmanager, will be broadcast when the shutdown time is set. This broadcastreceiver that we've implemented, when we receive this message, we're going to start the power off process.
B Custom action_request_power_on: Sets a rtc_wakeup clock that is set by Alarmmanager when Auto Power is on. We know that power on should set an RTC alarm, so what does this rtc_wakeup alarm do? In fact, when the user set the automatic shutdown, I set 2 clock, one is the RTC clock, for the shutdown state of the boot; another one is this rtc_wakeup clock. This clock is set up, in fact, such as you set up from Monday to Friday every 7:30 automatic boot, and Thursday morning you 7 o'clock opened the phone, so to the time of 7:30, the previous set up the clock expired, if not reset, Friday morning will not automatically boot. So this time, the previous set of rtc_wakeup received such information, reset the next time the automatic boot clock.
c) Boot_complete and timezone changed, time set-related action: You will need to reset the alarm when the system is powered on or when the time zone changes.
2 A service that handles power off, and when Broadcastreceiver receives Action_request_power_off, we give the user a chance to cancel the current automatic shutdown. The role of this service is to start a background-free page, to the user prompts. The beep or vibration that the user sets before playing.
3 An Activity: Display a dialog prompts the user to automatically shutdown, and with a timer countdown. When the user confirms the shutdown, or the timer to the time, the shutdown. Otherwise cancel the current shutdown and reset the next automatic shutdown alarm.
2. Automatic shutdown of the implementation. The implementation of automatic shutdown is relatively simple, here is the main way to set the alarm, and the implementation of the shutdown:
1 set the alarm of automatic shutdown:
The code is as follows |
Copy Code |
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 The automatic shutdown off is./frameworks/base/services/java/com/android/server/shutdownactivity.java:
The code is as follows |
Copy Code |
Intent newintent = new Intent (Intent.action_request_shutdown); Newintent.setflags (Intent.flag_activity_new_task); StartActivity (newintent); |
Intent.action_request_shutdown is a hidden ACTION inside the Intent.
3. Automatic power-on implementation. has been doing the upper level of application and framework, for the bottom is not very familiar. Just have colleagues to do before the clearance machine alarm, so put his previous implementation a little change on it. When the system power is off, we need to set up an RTC clock, and when the user is set to boot automatically, the clock is set by Alarmmanagerservice. This learns to support the bottom. The implementation here is to define one of our own RTC alarm type:
1 First in the header file to define:
A) kernel/include/linux/android_alarm.h
The code is as follows |
Copy Code |
#define ANDROID_ALARM_GET_TIME (Type) Alarm_iow (4, type, struct timespec) #define ANDROID_ALARM_SET_RTC _iow (' A ', 5, struct timespec) /* We define ANDROID_RTC_ALARM_SET for auto power off * * #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 after the definition is complete, also need to implement: in the Alarm_ioctl method of kernel/drivers/rtc/alarm-dev.c file, add a case, implement the setting alarm
The code is as follows |
Copy Code |
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 Failedn"); 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 } |
Of course don't forget to add an include:
The code is as follows |
Copy Code |
#include <mach/pmic.h> |
3 Add a method inside the frameworks/base/services/jni/com_android_server_alarmmanagerservice.cpp to set the clock:
The code is as follows |
Copy Code |
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:%sn", Seconds, Strerror (errno)); if (Result < 0) { LOGE ("Unable to set RTC alarm to%d later:%sn", Seconds, Strerror (errno)); } #endif } |
And just don't forget to define the interface:
The code is as follows |
Copy Code |
{"Updatertcalarm", "(II) V", (void*) Android_server_alarmmanagerservice_updatertcalarm}, |
4) in frameworks/base/services/java/com/android/server/ Alarmmanagerservice.java inside defines the native setting alarm method, and then the call can be implemented to set the alarm of the automatic shutdown down:
Definition: Private native void Updatertcalarm (int fd, int seconds);
Call:
The code is as follows |
Copy Code |
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 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 alarm } } |
5 in the application layer set the automatic boot
The code is as follows |
Copy Code |
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. Summary
1 The principle of automatic boot is relatively simple, but need to support the bottom, so for the application or framework layer of technical personnel, to achieve a little more trouble.
2 when setting up automatic switch machine, need to consider a lot of situations, such as whether to set time/timezone changes, the mobile phone is currently powered on or off the state.