在实现锁屏功能时能可能会出现这个问题出现…..这主要是因为锁屏需要管理员的权限…….
解决方法如下:
1、新建一个MyAdmin.java
package com.njupt.testrotate1;
import android.app.admin.DeviceAdminReceiver;
public class MyAdmin extends DeviceAdminReceiver {
}
2、在清单文件中如下配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.njupt.testrotate1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.njupt.testrotate1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.njupt.testrotate1.MyAdmin" >
<meta-data android:name="android.app.device_admin"
android:resource="@xml/my_admin"/>
<intent-filter >
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
3、在res目录下新建xml/my_admin.xml
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<!--
limit-password 设置密码的规则
watch-login 监控屏幕解锁尝试次数
reset-password 更改屏幕解锁密码
force-lock 设备自动锁屏
wipe-data 删除全部的数据
-->
<uses-policies>
<limit-password/>
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
4、在MainAcitivity中如下书写:
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的完整代码如下:
package com.njupt.testrotate1;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
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;
}
}