The realization of the timer switch machine in Android

Source: Internet
Author: User

About Android Auto-shutdown, there are many applications and examples on the web. Auto power off can be achieved by setting alarm in the application layer relative to automatic boot. With automatic boot, it requires the support of the underlying RTC clock. Introduction My implementation is to add an interface in the Setup program, let the user set up automatic switch machine, this automatic switch setting can refer to the alarm settings. Regarding the automatic shutdown, considering the shutdown, the user may be having some important operation, then should give the user a chance to cancel the current shutdown. 1) A broadcastreceiver, receive the following message: a) custom Actionrequestpoweroff: When setting auto power off, a rtcwakeup clock is set by Alarmmanager. When the shutdown time is set, the action previously set to Alarmmanager will be broadcast. The broadcastreceiver that we implement will start power off process B when this message is received, Actionrequestpoweron: When setting auto Power on, A rtcwakeup clock set by Alarmmanager. We know that power on should set an RTC alarm, so what does this rtcwakeup alarm do? In fact, when the user set up the automatic shutdown, I set up 2 clocks, one is the RTC clock, to power off the state, and one is this rtcwakeup clock. This clock is set, in fact, such as you set the Monday to Friday every day 7:30 automatic boot, and Thursday morning you opened the phone 7 o'clock, so to 7:30, the previous set of clocks expired, if not reset, Friday will not automatically boot up the morning.  So this time, before the Rtc_wakeup is set to receive this information, reset the next auto-boot clock. c) Boot_complete and timezone changed, time set, and other times-related action: Reset the alarm when the system is powered on or when the time zone changes. 2) A service that handles power off, when Broadcastreceiver receives Actionrequestpower_off, we give the user a chance to cancel the current auto-shutdown. The function of this service is to start a page without background, to give the user prompt. Play the beep or vibrate before the user sets. 3) An Activity: Displays a dialog prompting the user to automatically shut down and countdown with a timer. When usingWhen the user confirms the shutdown, or when the timer is in time, it shuts down the machine. Otherwise, cancel the current shutdown and reset the next auto-shutdown alarm. Automatic shutdown implementation. The implementation of automatic shutdown is relatively simple, here is how to set the alarm, and implement shutdown: 1) Set the automatic shutdown Alarm:[java]  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                &NBSP ;. Getsystemservice (Context.alarm_service);          Am.set (Alarmmanager.rtc_wakeup, Time, pendingintent); &NBSP;2) Auto shut down is./frameworks/base/services/java/com/android/server/shutdownacTivity.java:[java] intent newintent = new Intent (Intent.action_request_shutdown);      Newintent.setflags (intent.flag_activity_new_task);      StartActivity (newintent);  intent.actionrequestshutdown is a hidden action inside the Intent. The implementation of automatic boot. have been doing the upper application and the framework, not very familiar with the bottom. Just a colleague before the clearance machine alarm, so the implementation of his previous changes can be slightly. When the system power off is turned on automatically, we need to set up an RTC clock, which is set by the Alarmmanagerservice when the user sets up the automatic boot. This study needs the support of the bottom. The implementation here is to define one of our own RTC alarm type:1) First to define in the header file: a) Kernel/include/linux/android_alarm.h[java]   #define Android_ Alarm_get_time (type)       &NBSP;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, you also need to implement: in the Alarm_ioctl method of the Kernel/drivers/rtc/alarm-dev.c file, add a case to implement the settings alarm  [java] case Android_rtc_alarm_set:      {         unsigned int rtc_al Arm_time;          struct rtc_time rtc_now;          if (Copy_from_user (&rtc_alarm_time, (void __user *) arg,        & nbsp     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;             }         }          PM Ic_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 a Include:[java]   #include <mach/pmic.h>    3) Add a method to the frameworks/base/services/jni/comandroidserver_alarmmanagerservice.cpp to set the clock: [Java]  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 ALA RM to%d later:%s\n ", Seconds, Strerror (errno));        #endif  }    and don't forget to define the interface:  [java]  {"Updatertcalarm", "(II) V", ( void*) Android_server_alarmmanagerservice_updatertcalarm},    4) in frameworks/base/services/java/ Com/android/server/alarmmanagerservice.java inside the definition of native settings alarm method, and then call can implement the automatic shutdown of the alarm set down: definition: Private native void Updatertcalarm (int fd, int seconds); call: [Java]  public void setrepeating (int type, long triggerattime, long Inter Val,           pendingintent operation) {     if (operation = = null) {&nbsp ;        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) &nbs p;       }           //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    &NB Sp }  }   5) set the automatic boot on the application layer [Java] alarmmanager am = (alarmmanager) context              Getsystemservice (Context.alarm_service);      Intent Intent = new Intent (             "Com.android.settings.acti On. Request_power_on ");      Pendingintent pendingintent = pendingintent.getbroadcast (context, 0,        & nbsp     Intent, pendingintent.flag_cancel_current);      AM = (alarmmanager) context  www.2cto.com           . GETsystemservice (Context.alarm_service);      Am.set (Alarmmanager.elapsed_realtime_wakeup, Time, pendingintent);     Summary 1) The principle of automatic start-up is relatively simple, but requires the support of the bottom, so for the application or the framework layer of technical personnel, to achieve a little more trouble. 2) in the setting of the automatic switch machine, there are a lot of considerations, such as whether to set the time/timezone changes, the current phone is turned on or off the state and so on.

Implementation of the timer switch in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.