在实现锁屏功能的时候首先要获得管理员权限,然后通过用户授权,用户如果同意授权,则就可以修改或设置锁屏密码。
具体方法如下:
1,首先要新建一个DeviceAdminReceiver的子类
import android.app.admin.DeviceAdminReceiver;
public class AdminManageReceiver extends DeviceAdminReceiver {
}
2,在清单文件对接收者进行配置
package="com.njupt.testrotate1"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="8" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.njupt.testrotate1.MainActivity"
android:label="@string/app_name" >
android:resource="@xml/my_admin"/>
3,由于清单文件中用到了my_admin.xml,所以在在res目录下新建xml/my_admin.xml,内容如下:
然后在mainActivity中:
private DevicePolicyManager devicePolicyManager;
private boolean isAdminActive;
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// 申请权限
ComponentName componentName = new ComponentName(this, MyAdmin.class);
// 判断该组件是否有系统管理员的权限
isAdminActive = devicePolicyManager
.isAdminActive(componentName);
if(!isAdminActive){//这一句一定要有...
Intent intent = new Intent();
//指定动作
intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//指定给那个组件授权
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
startActivity(intent);
}
if(isAdminActive){
Toast.makeText(this, "具有权限,将进行锁屏....", 1).show();
devicePolicyManager.lockNow();
devicePolicyManager.resetPassword("123321", 0);
}
MainAcitivity的完整代码如下所示:
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import ponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements SensorEventListener {
private Button clear;
private TextView tv1;
private SensorManager mSensorManager;
private Vibrator vibrator;
private int counter = 1;
private DevicePolicyManager devicePolicyManager;
private boolean isAdminActive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
// clear = (Button) findViewById(R.id.clear);
// clear.setOnClickListener(new Button.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// clear.setText("现在给button赋值喽....");
// }
// });
//
tv1 = (TextView) findViewById(R.id.tv1);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// 申请权限
ComponentName componentName = new ComponentName(this, MyAdmin.class);
// 判断该组件是否有系统管理员的权限
isAdminActive = devicePolicyManager
.isAdminActive(componentName);
if(!isAdminActive){
Intent intent = new Intent();
//指定动作
intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//指定给那个组件授权
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
startActivity(intent);
}
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}
@Override
protected void onPause() {
mSensorManager.unregisterListener(this);
super.onPause();
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
float[] values = event.values;
float x = values[0];
float y = values[1];
if (sensorType == Sensor.TYPE_ACCELEROMETER) {
tv1.setText("现在的x轴是: " + x + " y轴是: " + y);
if (Math.abs(x) > 9.0 || Math.abs(y) > 9.0) {
// Toast.makeText(this, "现在的垂直方向已经超过了90度,将进行锁屏", 1).show();
vibrator.vibrate(500);
System.out.println("...............isAdminActive: "
+ isAdminActive);
if(isAdminActive){
Toast.makeText(this, "具有权限,将进行锁屏....", 1).show();
devicePolicyManager.lockNow();
devicePolicyManager.resetPassword("123321", 0);
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
如果觉得《android修改锁屏密码 Android修改 设置锁屏密码》对你有帮助,请点赞、收藏,并留下你的观点哦!