Android锁屏软件

时间:2023-03-09 05:42:26
Android锁屏软件

需求描述:锁屏软件就是点击应用然后直接锁屏,不会弹出任何界面

设计思路:应用启动以后通过getSystemService获取系统的服务,调用安卓系统的DevicePolicyManager和ComponentName来对系统进行操作,先判断用户是否有相应的权限,如果有,直接锁屏,如果没有就先获取系统权限,再锁屏

废话不说,直接上代码

Main.java(主页面)

 package com.example.wangshibang.screenlock;

 import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; /**
* Created by wangshibang on 2016-11-8.
*/
public class Main extends Activity {
private DevicePolicyManager policyManager;//管理和操作设备的API
private ComponentName componentName;//打开其他应用程序中的Activity或服务 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //获取设备管理服务
policyManager = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
//AdminReceiver 继承自 DeviceAdminReceiver
componentName = new ComponentName(this, AdminReceiver.class); //主方法
myLock(); //killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费
android.os.Process.killProcess(android.os.Process.myPid());
} private void myLock(){
boolean isActive = policyManager.isAdminActive(componentName);
if(!isActive){
//若没有权限就去获取权限
activeManage(); return;
//锁屏
//policyManager.lockNow();
}
else {
policyManager.lockNow();
}
} private void activeManage(){
//启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//权限列表
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//描述(additional explanation)
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "----Other Description----");
startActivityForResult(intent, 0);
}
}

AdminReceiver.java

 package com.example.wangshibang.screenlock;

 import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast; /**
* Created by wangshibang on 2016-11-8.
*/
public class AdminReceiver extends DeviceAdminReceiver { public static SharedPreferences getDevicePreference(Context context){
//获取设备存储的数值
return context.getSharedPreferences(DeviceAdminReceiver.class.getName(),0);
} // 密码的特点
public static String PREF_PASSWORD_QUALITY = "password_quality";
// 密码的长度
public static String PREF_PASSWORD_LENGTH = "password_length"; public static String PREF_MAX_FAILED_PW = "max_failed_pw"; void showToast(Context context, CharSequence text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
} @Override
public void onEnabled(Context context, Intent intent) {
// TODO Auto-generated method stub
showToast(context, "设备管理:可用");
} @Override
public void onDisabled(Context context, Intent intent) {
// TODO Auto-generated method stub
showToast(context, "Deactive successful!");
} @Override
public CharSequence onDisableRequested(Context context, Intent intent) {
// TODO Auto-generated method stub
return "Are you sure want to Deactive?";
} @Override
public void onPasswordChanged(Context context, Intent intent) {
// TODO Auto-generated method stub
showToast(context, "设备管理:密码己经改变");
} @Override
public void onPasswordFailed(Context context, Intent intent) {
// TODO Auto-generated method stub
showToast(context, "设备管理:改变密码失败");
} @Override
public void onPasswordSucceeded(Context context, Intent intent) {
// TODO Auto-generated method stub
showToast(context, "设备管理:改变密码成功");
}
}

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wangshibang.screenlock"> <application
android:allowBackup="true"
android:icon="@mipmap/lock"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 设备管理 -->
<receiver
android:name=".AdminReceiver"
android:label="ScreenLock"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/lock_screen" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
</application> </manifest>

res文件夹下建一个xml文件夹,里面有个lock_screen.xml文件

lock_screen.xml

 <?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<!-- 强行锁定 在里仅这个是需要的-->
<force-lock />
<!-- 清除所有数据(恢复出厂设置) -->
<wipe-data />
<!-- 重置密码 -->
<reset-password />
<!-- 限制密码选择 -->
<limit-password />
<!-- 监控登录尝试 -->
<watch-login />
</uses-policies>
</device-admin>

最后是什么都没有的activity_main.xml文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> </LinearLayout>

楼主才疏学浅,不喜勿喷

最后附上源码的链接:链接:http://pan.baidu.com/s/1i5x5s6x 密码:es42