Android开发之奇怪需求
2020-11-11 本文已影响0人
T92
最近的奇怪的需求越来越多了,简单举几个例子(只是其中最具代表性的几条):在设备未root的前提下实现后台全局截屏、重启设备、恢复出厂设置、更改设备密码、禁用状态栏下拉、静默安装卸载APP、隐藏第三方APP......WTF!!!
为了糊口,只能撸起袖子干!最终通过各种方式,除隐藏第三方APP外其他都实现了,这里记录几个,能写多少是多少吧(未root!)
重启设备
重启设备Google是提供了api的,但是需要APP为设备拥有者(DeviceOwner)才能调用,否则报错,而成为DeviceOwner需要借助PC写adb命令或者另一台手机实现,遂弃之,啥?adb!,那为啥不能用adb试试reboot命令!
首先写个执行命令的方法(搬砖):
public CommandResult execCommand(String[] commands, boolean isRoot) {
CommandResult commandResult = new CommandResult();
if (commands == null || commands.length == 0) return commandResult;
Process process = null;
DataOutputStream os = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
try {
process = Runtime.getRuntime().exec(isRoot ? "su" : "sh");
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command != null) {
os.write(command.getBytes());
os.writeBytes("\n");
os.flush();
}
}
os.writeBytes("exit\n");
os.flush();
commandResult.result = process.waitFor();
//获取错误信息
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) successMsg.append(s);
while ((s = errorResult.readLine()) != null) errorMsg.append(s);
commandResult.successMsg = successMsg.toString();
commandResult.errorMsg = errorMsg.toString();
Log.i("CommandExecution", commandResult.result + " | " + commandResult.successMsg
+ " | " + commandResult.errorMsg);
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e("CommandExecution", errmsg);
Map<String, String> map = new HashMap<>();
map.put("location","执行异常:" + errmsg);
invoke("location",map);
} else {
e.printStackTrace();
}
} catch (Exception e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e("CommandExecution", errmsg);
Map<String, String> map = new HashMap<>();
map.put("location","执行异常:" + errmsg);
invoke("location",map);
} else {
e.printStackTrace();
}
} finally {
try {
if (os != null) os.close();
if (successResult != null) successResult.close();
if (errorResult != null) errorResult.close();
} catch (IOException e) {
String errmsg = e.getMessage();
if (errmsg != null) {
Log.e("CommandExecution", errmsg);
} else {
e.printStackTrace();
}
}
if (process != null) process.destroy();
}
return commandResult;
}
private class CommandResult {
public int result = -1;
public String errorMsg;
public String successMsg;
}
然后执行(kotlin)
未root的手机可以实现部分adb命令,这里试了reboot不需要root权限,如果不行就写arrayOf("su" "-c" "reboot")三个命令依次执行
val cmds = arrayOf("reboot")
execCommand(cmds,false)//未root传false
以下代码就不放了,仅提供方向和思路
更改设备密码
研究一下DevicePolicyManager
,再补充补充DeviceAdmin
、ProfileOwner
、DeviceOwner
知识,很多系统控制就可以实现了
禁用状态栏下拉
用浮窗WindowManager
,LayoutParams
的type设置为TYPE_SYSTEM_ERROR
将浮窗置为最顶层阻止状态栏响应,间接实现禁用状态栏
静默安装卸载APP
这个除了root或者拿到系统源码or签名,否则我认为要实现真正静默是无解的,最终,通过Android系统的无障碍方式实现伪静默安装卸载
全局截屏
实现了,但不完美,虽然截屏成功,但会导致桌面停止运行,然后重新加载桌面,不值一提
隐藏第三方APP
求一个答案,在线等!挺急的