Android IDevicePolicyManager實現手動鎖屏

來源:互聯網
上載者:User

標籤:

Android手機一般不用時,都會通過電源鍵來鎖定螢幕同時關閉螢幕燈。

其實從API Level 8 (也就是Android 2.2) 開始, Android提供了DevicePolicyManager類, 可以讓你的應用程式也能執行螢幕鎖定等操作。

鎖定效果:

要讓自己的應用實現該螢幕鎖定,主要需要用到以下幾個類:DevicePolicyManager 
這是裝置管理的主類。通過它可以實現螢幕鎖定、螢幕亮度調節、出廠設定等功能。

DeviceAdminReceiver 
該類繼承自 BroadcastReceiver 。 從源碼可以看到,其實就是實現了一個OnReceive方法,該方法中根據不同的Action,執行相應的操作。 比如,如果啟用成功,那麼Action就是ACTION_DEVICE_ADMIN_ENABLED, 據此調用 onEnabled 方法。

系統源碼:

/**     * Intercept standard device administrator broadcasts.  Implementations     * should not override this method; it is better to implement the     * convenience callbacks for each action.     */    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        if (ACTION_PASSWORD_CHANGED.equals(action)) {            onPasswordChanged(context, intent);        } else if (ACTION_PASSWORD_FAILED.equals(action)) {            onPasswordFailed(context, intent);        } else if (ACTION_PASSWORD_SUCCEEDED.equals(action)) {            onPasswordSucceeded(context, intent);        } else if (ACTION_DEVICE_ADMIN_ENABLED.equals(action)) {            onEnabled(context, intent);        } else if (ACTION_DEVICE_ADMIN_DISABLE_REQUESTED.equals(action)) {            CharSequence res = onDisableRequested(context, intent);            if (res != null) {                Bundle extras = getResultExtras(true);                extras.putCharSequence(EXTRA_DISABLE_WARNING, res);            }        } else if (ACTION_DEVICE_ADMIN_DISABLED.equals(action)) {            onDisabled(context, intent);        }    }

具體中的簡單引用:

螢幕鎖定和恢復出廠預設值

1.建立 MyAdmin 的廣播接受者 繼承 DeviceAdminReceiver

        <receiver android:name=".MyAdmin">            <meta-data android:name="android.app.device_admin"                android:resource="@xml/my_admin" />            <intent-filter>                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />            </intent-filter>        </receiver>

my_admin.xml

<?xml version="1.0" encoding="utf-8"?><device-admin xmlns:android="http://schemas.android.com/apk/res/android">        <uses-policies>                <limit-password />                <watch-login />                <reset-password />                <force-lock />                <wipe-data />        </uses-policies></device-admin>

2.註冊廣播接受者為admin裝置

ComponentName mAdminName = new ComponentName(this, MyAdmin.class);if (mService != null) {        if (!mService.isAdminActive(mAdminName)) {                    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);                    intent.putExtra (DevicePolicyManager.EXTRA_DEVICE_ADMIN,mAdminName);                    startActivity(intent);                }}

3.擷取IDevicePolicyManager

DevicePolicyManager  dm =  (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);public void lock(View view){        dm.resetPassword("123", 0);  //滑動解鎖螢幕條後需要輸入密碼(方法中新設定的密碼)才能真正解鎖        dm.lockNow();        //恢復出廠預設值. //dm.wipeData(0);


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

<uses-permission android:name="android.permission.WAKE_LOCK"/



Android IDevicePolicyManager實現手動鎖屏

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.