标签:
package com.xfeng.onekeylock; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener { DevicePolicyManager devicePolicyManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); findViewById(R.id.btnLockScreen).setOnClickListener(this); findViewById(R.id.btnRegDeviceAdmin).setOnClickListener(this); findViewById(R.id.btnUnregDeviceAdmin).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btnLockScreen: devicePolicyManager.lockNow(); break; case R.id.btnRegDeviceAdmin: Intent i = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); i.putExtra(devicePolicyManager.EXTRA_DEVICE_ADMIN,new ComponentName(this,DeviceManagerBc.class)); startActivity(i); System.out.println("!!!!!"); break; case R.id.btnUnregDeviceAdmin: devicePolicyManager.removeActiveAdmin(new ComponentName(this,DeviceManagerBc.class)); System.out.println("!!3!!"); break; default: break; } } }
package com.xfeng.onekeylock; import android.app.admin.DeviceAdminReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * Created by xiaofeng on 15/12/30. */ 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(); } }
<?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=".MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="需要获取设备管理者权限才能拥有锁屏功能"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册成为系统组件" android:id="@+id/btnRegDeviceAdmin"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注销系统组件" android:id="@+id/btnUnregDeviceAdmin"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="锁屏" android:id="@+id/btnLockScreen"/> </LinearLayout>
<?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>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xfeng.onekeylock"> android:permission="android.permission.BIND_DEVICE_ADMIN" <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <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" /> </intent-filter> </receiver> </application> </manifest>
标签:
原文地址:http://www.cnblogs.com/xiaofeng6636/p/5093834.html