一键锁屏解锁卸载

Android 4.0

一键锁屏解锁卸载
锁屏,解锁,一键锁屏解锁

核心代码:
package cn.zengfansheng.screenlock;
 
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class MainActivity extends Activity implements OnClickListener {
 
    private static final int REQUEST_CODE_ENABLE_ADMIN = 110;
 
    private Button btn_screen_lock_on;
    private Button btn_screen_lock_off;
    private Button btn_screen_lock_uninstall;
    private Button btn_wipe_data;
 
    private DevicePolicyManager dpManager;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn_screen_lock_on = (Button) this.findViewById(R.id.btn_screen_lock_on);
        btn_screen_lock_off = (Button) this.findViewById(R.id.btn_screen_lock_off);
        btn_screen_lock_uninstall = (Button) this.findViewById(R.id.btn_screen_lock_uninstall);
        btn_wipe_data = (Button) this.findViewById(R.id.btn_wipe_data);
 
        dpManager = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
 
        btn_screen_lock_on.setOnClickListener(this);
        btn_screen_lock_off.setOnClickListener(this);
        btn_screen_lock_uninstall.setOnClickListener(this);
        btn_wipe_data.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_wipe_data:
            wipedata();
            break;
        case R.id.btn_screen_lock_on:
            screenOff();
            // System.out.println("屏幕锁屏可用并锁屏~~~");
            break;
        case R.id.btn_screen_lock_off:
            enableSceenLock();
            System.out.println("屏幕解锁不可用~~~");
            break;
        case R.id.btn_screen_lock_uninstall:
            uninstall();
            System.out.println("一键卸载~~~");
            break;
        }
    }
 
    /**
     * a、判断设备是否激活为管理设备
     * @return
     */

    private boolean isActiveAdmin() {
        ComponentName who = new ComponentName(this, ScreenLockDeviceAdminReceiver.class);
        return dpManager.isAdminActive(who);
    }
 
    /**
     * b、激活设备
     */

    public void activeAdminDevice() {
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        ComponentName who = new ComponentName(this, ScreenLockDeviceAdminReceiver.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,who);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,getResources().getString(R.string.screen_loack_desc));
        startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
    }
 
    /**
     * 清除用户数据,sd卡数据不会被清除
     */

    public void wipedata() {
        if (isActiveAdmin()) {
            dpManager.wipeData(0);
        } else {
            activeAdminDevice();
        }
    }
    /**
     *1、锁屏可用,并锁屏
     */

    public void screenOff() {
 
        if (isActiveAdmin()) {// a)如果激活了,那么锁屏 立即锁屏
            dpManager.lockNow();
            System.out.println("设备已激活,现在锁屏~~~");
        } else {// b)如果没有激活,提示用户激活设备
            System.out.println("设备未激活,现在进行设备激活~~~");
            activeAdminDevice();
        }
    }
    /**
     *2、锁屏不可用
     */

    public void enableSceenLock() {
        if (isActiveAdmin()) {
            ComponentName who = new ComponentName(this, ScreenLockDeviceAdminReceiver.class);
            dpManager.removeActiveAdmin(who );
        }
    }
    /**
     * 3、一键卸载
     */

    public void uninstall(){
        if (isActiveAdmin()) {// 已经激活
            ComponentName who = new ComponentName(this, ScreenLockDeviceAdminReceiver.class);
            dpManager.removeActiveAdmin(who);
        }
        /*<activity android:name=".UninstallerActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:excludeFromRecents="true"
            android:theme="@android:style/Theme.DeviceDefault.Dialog.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.DELETE" />
                <action android:name="android.intent.action.UNINSTALL_PACKAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="package" />
            </intent-filter>
        </activity>*/

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_UNINSTALL_PACKAGE);
        // intent.setAction(Intent.ACTION_DELETE);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivity(intent);
    }
}