下面是調用的相關代碼,主意許可權:
任何系統級應用都會到linux的系統調用中去, 對於reboot來講,會調用到int reboot (int mode) 或者直接用 __reboot來做, 我們只要change mode的值就ok,
mode 裡有:
#define RB_AUTOBOOT LINUX_REBOOT_CMD_RESTART
#define RB_HALT_SYSTEM LINUX_REBOOT_CMD_HALT
#define RB_ENABLE_CAD LINUX_REBOOT_CMD_CAD_ON
#define RB_DISABLE_CAD LINUX_REBOOT_CMD_CAD_OFF
#define RB_POWER_OFF LINUX_REBOOT_CMD_POWER_OFF
這些可以選擇,power off , autoboot 對應的就是開關機了。 當然您需要root或者system的許可權,或者SHELL好像也沒問題。
關機:
In frameworks/base/services/java/com/android/server/BatteryService.java
Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
重啟:
Intent i = new Intent(Intent.ACTION_REBOOT);
i.putExtra("nowait", 1);
i.putExtra("interval", 1);
i.putExtra("window", 0);
sendBroadcast(i);
下面是思路:
android 系統關機,重啟
1.android系統的關機,重啟代碼位於frameworks asecorejniandroid_os_Power.cpp,裡面有
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
{/*關機*/
sync();
#ifdef HAVE_ANDROID_OS
reboot(RB_POWER_OFF);
#endif
}
static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
{/*重啟*/
sync();
#ifdef HAVE_ANDROID_OS
if (reason == NULL) {
reboot(RB_AUTOBOOT);
} else {
const char *chars = env->GetStringUTFChars(reason, NULL);
__reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, (char*) chars);
env->ReleaseStringUTFChars(reason, chars); // In case it fails.
}
jniThrowIOException(env, errno);
#endif
}
2.最終會調用到linux中的sys_reboot,所以我們還是可以做到在android中加入重啟功能的;
關機和重啟在Linux Kernel中的哪些函數裡實現。
reboot的最終實現在arch_reset()函數,它通常定義在arch/arm/mach-xxx/include/mach/system.h裡。system.h被ARM kernel的public code應用,因此你需要定義這個標頭檔,並實現它。
arch_reset的函數原型為
void arch_reset(char mode)
void arch_reset(char mode)
關機需要將pm_power_off這個函數指標指向你自己的實現函數。函數指標是這樣定義的
void (*pm_power_off)(void); ......
http://yueguc.iteye.com/blog/1030305
android 系統關機,重啟
1.android系統的關機,重啟代碼位於frameworksasecorejniandroid_os_Power.cpp,裡面有
static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
{/*關機*/
sync();
#ifdef HAVE_ANDROID_OS
reboot(RB_POWER_OFF);
#endif
}
static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
{/*重啟*/
sync();
#ifdef HAVE_ANDROID_OS
if (reason == NULL) {
reboot(RB_AUTOBOOT);
} else {
const char *chars = env->GetStringUTFChars(reason, NULL);
__reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
LINUX_REBOOT_CMD_RESTART2, (char*) chars);
env->ReleaseStringUTFChars(reason, chars); // In case it fails.
}
jniThrowIOException(env, errno);
#endif
}
2.最終會調用到linux中的sys_reboot,所以我們還是可以做到在android中加入重啟功能的;
關機和重啟在Linux Kernel中的哪些函數裡實現。
reboot的最終實現在arch_reset()函數,它通常定義在arch/arm/mach-xxx/include/mach/system.h裡。system.h被ARM kernel的public code應用,因此你需要定義這個標頭檔,並實現它。
arch_reset的函數原型為
void arch_reset(char mode)
void arch_reset(char mode)
關機需要將pm_power_off這個函數指標指向你自己的實現函數。函數指標是這樣定義的
void (*pm_power_off)(void); ......