android 2.3.4代碼實現關機

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   java   ar   for   

開始在網上搜尋很多關於關機的程式。網上主流關機程式是在Java成發送要求關機的Intent.,如下:

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);  

 需要加上相關的許可權:

android:sharedUserId="android.uid.system"
<uses-permission android:name="android.permission.SHUTDOWN"/>
這裡涉及到一個問題:要在程式裡面加上系統程式許可權。如何在程式裡面加上系統許可權呢?
我的解決方案是先寫一個系統關機程式,產生shutdown.apk應用程式,然後項目代碼在調用這個shutdown.apk就可以完美關機了。
1.要獲得shutdown.apk應用程式。然而這個應用程式需要提升system app層級。擷取系統層級最好在源碼下編譯產生apk,
這樣很容易提升system app層級。
1.1、建立Shutdown工程。主要涉及ShutDownActivity.java和AndroidMainfest.xml.
1.2、ShutDownActivity.java源碼如下:
public class ShutDownActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Intent it = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");it.putExtra("android.intent.extra.KEY_CONFIRM", false);it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);startActivity(it);}}
1.3、AndroidMainfest.xml源碼如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.hutao.shutdown"    android:versionCode="1"   android:sharedUserId="android.uid.system"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" /><uses-permission android:name="android.permission.SHUTDOWN"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.hutao.shutdown.ShutDownActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
1.4、將建好的工程拷到/packages/apps/目錄下面,然後在現在源碼頂層source build/envsetup.sh。我的源碼頂層為
rowboat-android下面。然後將res下面的drawable-xxhdpi、values-sw600dp、value-sw720dp-land刪除。在
/rowboat-android/packages/apps/Shutdown/下面建立Android.mk檔案
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-java-files-under, src)LOCAL_PACKAGE_NAME := shutdownLOCAL_CERTIFICATE := platforminclude $(BUILD_PACKAGE)# Use the folloing include to make our test apk.include $(call all-makefiles-under,$(LOCAL_PATH))
然後在目前的目錄下進行mm命令編譯,在out/target/product/system/app/產生shutdown.apk
2.調用shutdown.apk關機。
2.1調用shutdown.apk代碼
Intent myIntent = new Intent();  myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  myIntent.setClassName("com.hutao.shutdown",   "com.hutao.shutdown.ShutDownActivity");  startActivity(myIntent);
2.2在調用關機介面時候有兩種形式:
即it.putExtra("android.intent.extra.KEY_CONFIRM", false);//false不顯示確認關機的對話方塊,直接關機
或it.putExtra("android.intent.extra.KEY_CONFIRM", true);//true顯示確認關機的對話方塊,讓使用者選擇是否確認關機

 


 



 


 



android 2.3.4代碼實現關機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.