Android 實(shí)現(xiàn)關(guān)機(jī)的多種方式
想要在代碼中實(shí)現(xiàn)關(guān)機(jī)需要 apk 有系統(tǒng)權(quán)限,要在 manifest 文件添加 android:sharedUserId=“android.uid.system”,還要有系統(tǒng)簽名。
第一種方式直接使用 adb shell 命令,調(diào)用 reboot 命令來關(guān)機(jī)
try { Runtime.getRuntime().exec('reboot -p'); //關(guān)機(jī)} catch (IOException e) { e.printStackTrace();}第二種方式
調(diào)用 PowerManage 中的 shutdown 方法,但是該方法是隱藏的 API,通過反射即可調(diào)用,代碼如下:
try { PowerManager pManager = (PowerManager) VfiServiceApp.getContext().getSystemService(Context.POWER_SERVICE); if (pManager != null) {Method method = pManager.getClass().getMethod('shutdown', boolean.class, String.class, boolean.class);method.invoke(pManager, false, null, false); }} catch (Exception e) { e.printStackTrace();}第三種方式
發(fā)送廣播
Broadcast
Intent.ACTION_REQUEST_SHUTDOWN關(guān)機(jī)廣播Intent.ACTION_REBOOT重啟廣播
ACTION_REQUEST和ACTION_REBOOT是Intent.java是聲明的兩個(gè)字符串常量,系統(tǒng)接收到這兩個(gè)廣播后就會(huì)響應(yīng)關(guān)機(jī)或重啟的操作。源碼中的實(shí)現(xiàn)如下:聲明代碼路徑:/frameworks/base/core/java/android/content/Intent.java
public static final String ACTION_REQUEST_SHUTDOWN = 'android.intent.action.ACTION_REQUEST_SHUTDOWN'public static final String ACTION_REBOOT = 'android.intent.action.REBOOT'權(quán)限: AndroidMenifest.xml中添加代碼
android:sharedUserId='android.uid.system'提升至系統(tǒng)權(quán)限<uses-permission android:name='android.permission.SHUTDOWN' />添加關(guān)機(jī)權(quán)限
需要在源碼中編譯項(xiàng)目,所以需要在項(xiàng)目根目錄下添加Android.mk文件:LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_PACKAGE_NAME := PowerActionDemo LOCAL_CERTIFICATE := platform include $(BUILD_PACKAGE) 第四種方式
通過init.rc啟動(dòng)系統(tǒng)服務(wù)來運(yùn)行sh文件
Android文件系統(tǒng)啟動(dòng)后首先調(diào)用/init,init文件會(huì)解析init.rc和init.xxx.rc然后執(zhí)行,init.rc會(huì)在系統(tǒng)初始化過程中做一些簡(jiǎn)單的初始化操作,可以利用init過程解析自己加進(jìn)去的關(guān)機(jī)或重啟腳本。
編寫關(guān)機(jī)或重啟腳本sh文件
#!/system/bin/shreboot
#!/system/bin/shreboot -p #或者shutdown
編寫mk文件
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_PREBUILT_EXECUTABLES := system_shutdown.sh system_reboot.sh LOCAL_MODULE_TAGS := optional include $(BUILD_MULTI_PREBUILT)
修改init.rc文件,在文件最后添加如下:
service system_shutdown /system/bin/system_shutdown.sh #第一步中的sh文件名 oneshot #只啟動(dòng)一次disabled #禁用服務(wù),不會(huì)開機(jī)自啟動(dòng),但是可以在應(yīng)用程序中手動(dòng)啟動(dòng) service system_reboot /system/bin/system_reboot.sh oneshot disabled
新建目錄,將上面的mk文件和兩個(gè)sh腳本放到目錄下,然后將文件夾副指導(dǎo)系統(tǒng)路徑中,然后編譯源碼。即可在代碼中調(diào)用系統(tǒng)服務(wù)重啟或關(guān)機(jī)
SystemProperties.set('ctl.start', 'system_shutdown'); //system_shutdown是sh腳本的文件名SystemProperties.set('ctl.start', 'system_reboot'); 第五種方式
通過init.rc啟動(dòng)系統(tǒng)服務(wù)來運(yùn)行sh文件(也是最常用的方法)
//Runtime執(zhí)行l(wèi)inux-shell case R.id.shutdown_btn3: try{ Log.v(TAG, 'root Runtime->shutdown'); //Process proc =Runtime.getRuntime().exec(new String[]{'su','-c','shutdown'}); //關(guān)機(jī) Process proc =Runtime.getRuntime().exec(new String[]{'su','-c','reboot -p'}); //關(guān)機(jī) proc.waitFor(); }catch(Exception e){ e.printStackTrace(); } break; case R.id.reboot_btn3: try { Log.v(TAG, 'root Runtime->reboot'); Process proc =Runtime.getRuntime().exec(new String[]{'su','-c','reboot '}); //關(guān)機(jī) proc.waitFor(); }catch (Exception ex){ ex.printStackTrace(); } break;
前提是android系統(tǒng)system/bin 目錄下存在reboot和shutdown文件,大多數(shù)型號(hào)的設(shè)備都是有的。設(shè)備需要獲取root權(quán)限。
第六種方式PowerManager提供了reboot接口
PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE); pManager.reboot(null);//重啟
以上就是Android 實(shí)現(xiàn)關(guān)機(jī)的多種方式的詳細(xì)內(nèi)容,更多關(guān)于Android 實(shí)現(xiàn)關(guān)機(jī)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
