Android關機重啟實現

kv110發表於2014-09-19

 Android關機重啟實現

 

Android通常是在長按Power鍵後出現彈出選單,然後可以選擇Power Off進行關機操作。程式碼實現在

frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java(mPowerLongPressRunnable)

frameworks/policies/base/phone/com/android/internal/policy/impl/GlobalActions.java

下面是個示例程式實現關機重啟和恢復出廠設定,在KK上測試通過

1. Android.mk

LOCAL_PACKAGE_NAME := PowerOff

LOCAL_CERTIFICATE := platform

 

2. AndroidManifest.xml

    package="com.***.poweroff"

    android:sharedUserId="android.uid.system"   

    <uses-permission android:name="android.permission.SHUTDOWN"/>

    <uses-permission android:name="android.permission.MASTER_CLEAR" />

    <application

        android:icon="@drawable/icon"

        android:label="@string/app_name" >

        <activity

            android:name=".PowerOff"

            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>

 

3. PowerOff.java

 ok_poweroff.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

        Log.d(TAG, "Power off starting");

        Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);

        intent.putExtra(Intent.EXTRA_KEY_CONFIRM, true);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);

            }

        });

 

      ok_restart.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

        Log.d(TAG, "Restarting");

        Intent intent = new Intent(Intent.ACTION_REBOOT);

        intent.putExtra("nowait", 1);

        intent.putExtra("interval", 1);

        intent.putExtra("window", 0);

        sendBroadcast(intent);

            }

        });

 

        ok_factoryreset.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

              Log.d(TAG, "Factory reset starting");

              sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));

            }

        });

 

相關文章