【Android开发小记--10】一键锁屏功能实现

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014252406/article/details/50641480

效果图:


             

这里对该应用配置两个启动图标,即对两个activity进行配置:


<activity ...  <strong>android:process=":process.main"</strong> >
   ......
<activity ...  <strong>android:process=":process.sub"  android:launchMode="singleInstance"</strong> >
    <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>


这里需要配置一个广播接收器,侦听当前应用是否有系统组件的能力:


/**
 * 广播接收器,侦听当前应用是否有系统组件的能力
 */
public class DeviceManagerBC extends DeviceAdminReceiver {
    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Toast.makeText(context, "已获取设备管理者权限", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Toast.makeText(context, "您取消了设备管理者权限", Toast.LENGTH_SHORT).show();
    }
}

同时,在 res/xml 文件夹下,新建 device_admin.xml 文件,配置锁屏权限:

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


并且在AndroidManifest.xml中配置 receiver:

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

设置布局文件,为层叠布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.dingding.onekeylock.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text" />
    
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/get_admin"
        android:id="@+id/btnAdmin"
        android:layout_gravity="center_horizontal" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:id="@+id/btnCancel"
        android:visibility="gone"
        android:layout_gravity="center_horizontal" />
    
</FrameLayout>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/lock_screen"
        android:id="@+id/btnLock"
        android:visibility="gone"
        android:layout_gravity="center_horizontal" />
</LinearLayout>


主 Activity中,根据权限是否获取来确定按钮的显示:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnAdmin;
    private Button btnCancel;
    private Button btnLock;
    private DevicePolicyManager devicePolicyManager;
    private static final int DEVICE_POLICY_MANAGER_REQUEST_CODE = 21;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        btnAdmin = (Button) findViewById(R.id.btnAdmin);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        btnLock = (Button) findViewById(R.id.btnLock);
        //按钮监听
        btnAdmin.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        btnLock.setOnClickListener(this);
        //用来判断是否已经配置权限
        if (devicePolicyManager.isAdminActive(new ComponentName(this, DeviceManagerBC.class))) {
            pageSetAdmin();
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAdmin:
                //获取权限
                Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                i.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, DeviceManagerBC.class));
                //替换startActivity(i),对取消也作出反应
                startActivityForResult(i, DEVICE_POLICY_MANAGER_REQUEST_CODE);
                break;
            case R.id.btnCancel:
                //取消权限
                devicePolicyManager.removeActiveAdmin(new ComponentName(this, DeviceManagerBC.class));
                pageLock();
                break;
            case R.id.btnLock:
                devicePolicyManager.lockNow();//锁屏
                break;
        }
    }
    /*设置界面后切换*/
    public void pageSetAdmin() {
        btnAdmin.setVisibility(View.INVISIBLE);
        btnCancel.setVisibility(View.VISIBLE);
        btnLock.setVisibility(View.VISIBLE);
    }
    /*取消权限后界面切换*/
    public void pageLock() {
        btnAdmin.setVisibility(View.VISIBLE);
        btnCancel.setVisibility(View.INVISIBLE);
        btnLock.setVisibility(View.INVISIBLE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case RESULT_OK:
                pageSetAdmin();
                break;
            case RESULT_CANCELED:
                break;
        }
    }
}


在第二个启动界面中,若已获得权限,点击图标后则直接锁屏,否则,直接进入主界面:

/*第二个启动界面*/
public class OneKeyLockActivity extends AppCompatActivity {
    private DevicePolicyManager devicePolicyManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        try {
            devicePolicyManager.lockNow();
            finish();
        } catch (Exception e) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }
}


具体代码点击





展开阅读全文

没有更多推荐了,返回首页