Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android 設備管理員

Android 設備管理員

編輯:Android開發實例

  Android 2.2 SDK提供了一個可管理和操作設備的API叫DevicePolicyManager,使用這個API你可以接管手機的應用權限,對手機做出很多大膽的操作,比如鎖屏,恢復出廠設置(這麼和諧的東西要是在中國是不大可能提供給你的),還有設置密碼、強制清除密碼,修改密碼、設置屏幕燈光漸暗時間間隔等操作。這個API可謂是直接可以將你做的應用程序變成系統的老大哥了。雖說是這樣,但應用程序可做老大只是對於你本身應用程序有效,別人也可以做類似的應用程序,這個與別人的權限是不起沖突的。

  好了,廢話不說,上界面:

具體的編寫代碼的流程:

1.因為這個API是用的2.2提供的API,所以必須將sdkVersion設置為8,像這樣<uses-sdk android:minSdkVersion="8" />,這是必須的。

2.注冊一個廣播服務類,用以監聽權限的變化:

 

<receiver android:name=".deviceAdminReceiver" android:label="@string/app_name"
            android:description="@string/description" android:permission="android.permission.BIND_DEVICE_ADMIN">

            <meta-data android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            </intent-filter>

        </receiver>

 

android:permission 表示此功能需要的權限,android:name="android.app.action.DEVICE_ADMIN_ENABLED" 表示此動作的跳轉界面

<meta-data android:name="android.app.device_admin"
                android:resource="@xml/device_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>

 

 

廣播服務類的JAVA代碼,重寫一些必要的實現函數:

 

package com.terry.device;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

public class deviceAdminReceiver extends DeviceAdminReceiver {

    /**
     * 獲取設備存儲的數值
     * 
     * @param context
     * @return
     */
    public static SharedPreferences getDevicePreference(Context context) {
        return context.getSharedPreferences(
                DeviceAdminReceiver.class.getName(), 0);
    }

    // 密碼的特點
    public static String PREF_PASSWORD_QUALITY = "password_quality";
    // 密碼的長度
    public static String PREF_PASSWORD_LENGTH = "password_length";

    public static String PREF_MAX_FAILED_PW = "max_failed_pw";

    void showToast(Context context, CharSequence text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onEnabled(Context context, Intent intent) {
        // TODO Auto-generated method stub
        showToast(context, "設備管理:可用");
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        // TODO Auto-generated method stub
        showToast(context, "設備管理:不可用");
    }

    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        // TODO Auto-generated method stub
        return "這是一個可選的消息,警告有關禁止用戶的請求";
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        // TODO Auto-generated method stub
        showToast(context, "設備管理:密碼己經改變");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        // TODO Auto-generated method stub
        showToast(context, "設備管理:改變密碼失敗");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        // TODO Auto-generated method stub
        showToast(context, "設備管理:改變密碼成功");
    }

}

 

 

DeviceAdminReceiver是擴展於BroadcastReceiver。

 

下面先來看看操作效果,點擊啟用管理:

數據顯示不完全 ,往下拉將看到:

中間有一句話叫做:“設備管理可以對系統的一些安全進行設置”,這句話來源於上面的廣播receiver中的description:android:description="@string/description",點擊激活會觸發廣播類響應上面廣播類彈出Toast。啟用管理代碼:

 

/**
     * 設備管理可用的點擊事件
     * 
     * @author terry
     * 
     */
    class enableAdminClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mDeviceComponentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "這裡可以輸入一些額外的說明,比如提示用戶什麼的");
            startActivityForResult(intent, RESULT_ENABLE);
        }

    }

 

 

 

鎖屏操作,由於是模擬器不能做到真正錯屏,只能停到初始模擬器進來需要解鎖的狀態,屏幕不會變暗。鎖屏代碼:

 

/**
     * 鎖屏
     * 
     * @author terry
     * 
     */
    class force_lock implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mAM.isUserAMonkey()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        deviceActivity.this);
                builder.setMessage("你不能對此屏幕進行操作,因為你不是管理員");
                builder.setPositiveButton("I admit defeat", null);
                builder.show();
                return;
            }
            boolean active = mDPM.isAdminActive(mDeviceComponentName);
            if (active) {
                mDPM.lockNow();
            }
        }
    }

 

 

屏幕在設置相應時間後燈光變暗效果:

 

/**
     * 屏幕自動變暗
     * 
     * @author terry
     * 
     */
    class timeoutClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mAM.isUserAMonkey()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        deviceActivity.this);
                builder.setMessage("你不能對我的屏幕進行操作,因為你不是管理員");
                builder.setPositiveButton("I admit defeat", null);
                builder.show();
                return;
            }
            boolean active = mDPM.isAdminActive(mDeviceComponentName);
            if (active) {
                long timeout = 1000L * Long.parseLong(et.getText().toString());
                mDPM.setMaximumTimeToLock(mDeviceComponentName, timeout);
            }
        }
    }

 

 

由於是模擬器,恢復出廠設置清除數據後,將無法重新開機,必須重新啟動機子,在真機上是沒有問題的,測試的時候必須小心,以免將你的數據清除掉。恢復出廠設置代碼:

 

/**
     * 恢復出廠設置
     * 
     * @author terry
     * 
     */
    class resetClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mAM.isUserAMonkey()) {
                // Don't trust monkeys to do the right thing!
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        deviceActivity.this);
                builder
                        .setMessage("You can't wipe my data because you are a monkey!");
                builder.setPositiveButton("I admit defeat", null);
                builder.show();
                return;
            }

            AlertDialog.Builder builder = new Builder(deviceActivity.this);
            builder.setMessage("將重置數據,你確定此操作嗎?");
            builder.setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            AlertDialog.Builder aler = new AlertDialog.Builder(
                                    deviceActivity.this);
                            aler.setMessage("刪除數據後,系統將會重新啟動.確定嗎?");
                            aler.setPositiveButton(android.R.string.ok,
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub
                                            boolean active = mDPM
                                                    .isAdminActive(mDeviceComponentName);
                                            if (active) {
                                                mDPM.wipeData(0);
                                            }
                                        }
                                    });
                            aler
                                    .setNeutralButton(android.R.string.cancel,
                                            null);
                            aler.show();
                        }
                    });
            builder.setNeutralButton(android.R.string.cancel, null);
            builder.show();
        }
    }

 

API的提供簡化了很多煩瑣的操作,就上面那些代碼就可以實現了,完整代碼如下:

 

DeviceActivity package com.terry.device;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class deviceActivity extends Activity {
    static final int RESULT_ENABLE = 1;
    DevicePolicyManager mDPM;
    ActivityManager mAM;
    ComponentName mDeviceComponentName;
    Button enableAdmin, disableAdmin, force_lock, btn_time_out, reset;
    EditText et;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mAM = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        mDeviceComponentName = new ComponentName(deviceActivity.this,
                deviceAdminReceiver.class);
        setContentView(R.layout.main);

        findView();

        init();

    }

    void findView() {
        enableAdmin = (Button) findViewById(R.id.enable_admin);
        disableAdmin = (Button) findViewById(R.id.disable_admin);
        force_lock = (Button) findViewById(R.id.force_lock);
        btn_time_out = (Button) findViewById(R.id.time_out);
        et = (EditText) findViewById(R.id.et_time_out);
        reset = (Button) findViewById(R.id.reset);
    }

    void init() {
        enableAdmin.setOnClickListener(new enableAdminClickEvent());
        disableAdmin.setOnClickListener(new disableAdminClickEvent());
        force_lock.setOnClickListener(new force_lock());
        btn_time_out.setOnClickListener(new timeoutClickEvent());
        reset.setOnClickListener(new resetClickEvent());
    }

    void updateButtonState() {
        boolean active = mDPM.isAdminActive(mDeviceComponentName);
        if (active) {
            enableAdmin.setEnabled(false);
            disableAdmin.setEnabled(true);
            force_lock.setEnabled(true);
            btn_time_out.setEnabled(true);
            reset.setEnabled(true);
        } else {
            enableAdmin.setEnabled(true);
            disableAdmin.setEnabled(false);
            force_lock.setEnabled(false);
            btn_time_out.setEnabled(false);
            reset.setEnabled(false);
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        updateButtonState();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        switch (requestCode) {
        case RESULT_ENABLE:
            if (resultCode == Activity.RESULT_OK) {
                Log.v("DeviceEnable", "deviceAdmin:enable");
            } else {
                Log.v("DeviceEnable", "deviceAdmin:disable");
            }
            break;

        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 設備管理可用的點擊事件
     * 
     * @author terry
     * 
     */
    class enableAdminClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    mDeviceComponentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "這裡可以輸入一些額外的說明,比如提示用戶什麼的");
            startActivityForResult(intent, RESULT_ENABLE);
        }

    }

    /**
     * 設備管理不可用的點擊事件
     * 
     * @author terry
     * 
     */
    class disableAdminClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mDPM.removeActiveAdmin(mDeviceComponentName);
            updateButtonState();
        }

    }

    /**
     * 鎖屏
     * 
     * @author terry
     * 
     */
    class force_lock implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mAM.isUserAMonkey()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        deviceActivity.this);
                builder.setMessage("你不能對此屏幕進行操作,因為你不是管理員");
                builder.setPositiveButton("I admit defeat", null);
                builder.show();
                return;
            }
            boolean active = mDPM.isAdminActive(mDeviceComponentName);
            if (active) {
                mDPM.lockNow();
            }
        }
    }

    /**
     * 屏幕自動變暗
     * 
     * @author terry
     * 
     */
    class timeoutClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mAM.isUserAMonkey()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        deviceActivity.this);
                builder.setMessage("你不能對我的屏幕進行操作,因為你不是管理員");
                builder.setPositiveButton("I admit defeat", null);
                builder.show();
                return;
            }
            boolean active = mDPM.isAdminActive(mDeviceComponentName);
            if (active) {
                long timeout = 1000L * Long.parseLong(et.getText().toString());
                mDPM.setMaximumTimeToLock(mDeviceComponentName, timeout);
            }
        }
    }

    /**
     * 恢復出廠設置
     * 
     * @author terry
     * 
     */
    class resetClickEvent implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mAM.isUserAMonkey()) {
                // Don't trust monkeys to do the right thing!
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        deviceActivity.this);
                builder
                        .setMessage("You can't wipe my data because you are a monkey!");
                builder.setPositiveButton("I admit defeat", null);
                builder.show();
                return;
            }

            AlertDialog.Builder builder = new Builder(deviceActivity.this);
            builder.setMessage("將重置數據,你確定此操作嗎?");
            builder.setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            AlertDialog.Builder aler = new AlertDialog.Builder(
                                    deviceActivity.this);
                            aler.setMessage("刪除數據後,系統將會重新啟動.確定嗎?");
                            aler.setPositiveButton(android.R.string.ok,
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub
                                            boolean active = mDPM
                                                    .isAdminActive(mDeviceComponentName);
                                            if (active) {
                                                mDPM.wipeData(0);
                                            }
                                        }
                                    });
                            aler
                                    .setNeutralButton(android.R.string.cancel,
                                            null);
                            aler.show();
                        }
                    });
            builder.setNeutralButton(android.R.string.cancel, null);
            builder.show();
        }
    }

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved