In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve automatic switch in Android. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
1. Brief introduction
My implementation is to add an interface in the setting program to allow users to set the automatic switch, which can be set according to the setting of the alarm clock. With regard to automatic shutdown, considering that the user may be having some important actions at the time of shutdown, the user should be given a chance to cancel the current shutdown.
1) A BroadcastReceiver that receives the following information:
A) Custom ACTION_REQUEST_POWER_OFF: a RTC_WAKEUP clock set through AlarmManager when setting auto power off. When it comes to the set shutdown time, the action previously set to AlarmManager will be broadcast. After the BroadcastReceiver we implemented receives this message, we will start the power off process.
B) Custom ACTION_REQUEST_POWER_ON: when setting auto power on, a RTC_WAKEUP clock is set through AlarmManager. We know that power on should set up a rtc alarm, so what does the alarm of this RTC_WAKEUP do? In fact, when the user sets the automatic shutdown, I set two clocks, one is the RTC clock, which is used to boot in the shutdown mode, and the other is the RTC_WAKEUP clock. The reason for setting this clock is actually like this. For example, you set to automatically turn on the phone at 7: 30 every day from Monday to Friday, and you turn on your phone at 7: 00 on Thursday morning, so that by 07:30, the previously set clock will expire. If you don't reset it, it won't turn on automatically on Friday morning. So at this time, the previously set RTC_WAKEUP received such a message and reset the clock for the next automatic boot.
C) time-related action such as BOOT_COMPLETE and TIMEZONE changed, Time set: when the system starts up or the time and time zone changes, the alarm needs to be reset.
2) A Service that handles power off. When the BroadcastReceiver receives the ACTION_REQUEST_POWER_OFF, we give the user an opportunity to cancel the current automatic shutdown. The purpose of this Service is to launch a background-free page and give the user a hint. At the same time, play the cue tone or vibration set by the user.
3) An Activity: displays a dialog that prompts the user to shut down automatically and countdown with a timer. When the user confirms that the phone is turned off, or when the timer is up, the phone is turned off. Otherwise, cancel the current shutdown and reset the next automatic shutdown alarm.
two。 Realization of automatic shutdown
The implementation of automatic shutdown is relatively simple. Here we mainly talk about how to set alarm and how to achieve shutdown:
1) set the alarm for automatic shutdown:
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) automatic shutdown is adjusted as follows:
. / frameworks/base/services/java/com/android/server/ShutdownActivity.javaIntent newIntent = newIntent (Intent.ACTION_REQUEST_SHUTDOWN); newIntent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); startActivity (newIntent)
Intent.ACTION_REQUEST_SHUTDOWN is a hidden action in Intent.
3. Realization of automatic boot
Has been doing the upper application and framework, not very familiar with the bottom. It just so happens that a colleague has turned off the alarm before, so just make some changes to his previous implementation. When the system starts automatically in the state of power off, we need to set a rtc clock. When the user sets the auto-boot, the AlarmManagerService sets the clock down. This study needs the support of the bottom. The implementation here is to define our own rtc alarm type:
1) first, define it in the header file:
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 ('averse, 5, struct timespec) # define ANDROID_RTC_ALARM_SET _ IOW (' await, 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 ('asides, 7, int)
2) after the definition is completed, you also need to implement: in the alarm_ioctl method of the kernel/drivers/rtc/alarm-dev.c file, add a case to set alarm:
Case ANDROID_RTC_ALARM_SET: {unsigned int rtc_alarm_time; struct rtc_time rtc_now; if (& 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;}
Of course, don't forget to add an include:
# include
3) add a method to set the clock in frameworks/base/services/jni/com_android_server_AlarmManagerService.cpp:
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}
And don't forget to define the interface:
{"updateRtcAlarm", "(II) V", (void*) android_server_AlarmManagerService_updateRtcAlarm}
4) define the method of setting alarm for native in frameworks/base/services/java/com/android/server/AlarmManagerService.java, and then call it to set the alarm for automatic shutdown:
Definition: private native void updateRtcAlarm (int fd, int seconds)
Call:
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) set automatic boot at the application layer:
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); these are all the contents of the article "how to switch on and off automatically in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.