1.判断进程是否存在
//判断是否在主进程,这个方法判断进程名或者pid都可以,如果进程名一样那pid肯定也一样
//true:当前进程是主进程 false:当前进程不是主进程
public boolean isUIProcess() {
ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
List processInfos =
am.getRunningAppProcesses();
String mainProcessName = getPackageName();
int myPid = android.os.Process.myPid();
for (ActivityManager.RunningAppProcessInfo info : processInfos) {
if (info.pid == myPid && mainProcessName.equals(info.processName)) {
return true;
}
}
return false;
}
2.判断服务是否存在
/**
* 判断service是否已经运行
* 必须判断uid,因为可能有重名的Service,所以要找自己程序的Service,不同进程只要是同一个程序就是同一个uid,个人理解android系统中一个程序就是一个用户
* 用pid替换uid进行判断强烈不建议,因为如果是远程Service的话,主进程的pid和远程Service的pid不是一个值,在主进程调用该方法会导致Service即使已经运行也会认为没有运行
* 如果Service和主进程是一个进程的话,用pid不会出错,但是这种方法强烈不建议,如果你后来把Service改成了远程Service,这时候判断就出错了
*
* @param className Service的全名,例如PushService.class.getName()
* @return true:Service已运行 false:Service未运行
*/
public boolean isServiceExisted(String className) {
ActivityManager am = (ActivityManager)
getSystemService(Context.ACTIVITY_SERVICE);
List serviceList =
am.getRunningServices(Integer.MAX_VALUE);
int myUid = android.os.Process.myUid();
for (ActivityManager.RunningServiceInfo runningServiceInfo : serviceList) {
if (runningServiceInfo.uid == myUid &&
runningServiceInfo.service.getClassName().equals(className)) {
return true;
}
}
return false;
}
3.app是否在前台
// 是否在前台
private boolean isAppForeground(String packageName) {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if (!TextUtils.isEmpty(currentPackageName)
&& currentPackageName.equals(packageName)) {
return true;
}
return false;
}
把应用置为前台
moveTaskToFront()
moveTaskToBack()
private void bring2Front() {
ActivityManager activtyManager = (ActivityManager)
getSystemService(Context.ACTIVITY_SERVICE);
List runningTaskInfos =
activtyManager.getRunningTasks(3);
for (ActivityManager.RunningTaskInfo runningTaskInfo : runningTaskInfos) {
if (this.getPackageName().equals(runningTaskInfo.topActivity.getPackageName())) {
activtyManager.moveTaskToFront(
runningTaskInfo.id, ActivityManager.MOVE_TASK_WITH_HOME);
return;
}
}
}
回到系统桌面
private void back2Home() {
Intent home = new Intent(Intent.ACTION_MAIN);
home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
}
4.手机锁
private boolean isScreenLocked() {
KeyguardManager keyguardManager =
(KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.inKeyguardRestrictedInputMode();
}
private void wakeAndUnlock() {
//获取电源管理器对象
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
//获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是调试用的Tag
PowerManager.WakeLock wl = pm
.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
//点亮屏幕
wl.acquire(1000);
//得到键盘锁管理器对象
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
kl = km.newKeyguardLock("unLock");
//解锁
kl.disableKeyguard();
}
private void release() {
if (locked && kl != null) {
android.util.Log.d("maptrix", "release the lock");
//得到键盘锁管理器对象
kl.reenableKeyguard();
locked = false;
}
}
5.系统app
public void onclick(View view) {
DataOutputStream dataOutputStream = null;
BufferedReader err = null;
try {
//得到su权限
Process process = Runtime.getRuntime().exec("su root");
//输出命令
dataOutputStream = new DataOutputStream(process.getOutputStream());
//获取外部sd路径
File externalStorageDirectory = Environment.getExternalStorageDirectory();
//得到apk文件路径
File apkFile = new File(externalStorageDirectory
.getAbsoluteFile()+File.separator+"youjiayou.apk");
//移动文件命令
String command = "cp " + apkFile.getAbsolutePath() + " system/app\n";
//挂载为system可写 不然无法写入文件 = = 这些坑我是一点点 踩过的
dataOutputStream.writeBytes("mount -o remount rw /system\n");
//输出命令
dataOutputStream.write(command.getBytes("UTF-8"));
//挂载为可写不然无法生效
dataOutputStream.writeBytes("chmod 777 /system/app/youjiayou.apk\n");
//挂载为可读
dataOutputStream.writeBytes("mount -o remount ro /system\n");
//刷新输出
dataOutputStream.flush();
//重启
dataOutputStream.writeBytes("reboot\n");
//退出
dataOutputStream.writeBytes("exit\n");
//刷新输出
dataOutputStream.flush();
//等候命令结束
process.waitFor();
String line;
String msg = "";
//读取控制台返回的数据
err = new BufferedReader(new InputStreamReader(process.getInputStream()));
//读取数据
while ((line = err.readLine()) != null) {
msg += line;
}
Log.e("Fmy", "结果 " +msg);
} catch (Exception e) {
// e.printStackTrace();
Log.e("Fmy", "发生异常" + e.getMessage());
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
if (err != null) {
err.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:
2020/05/15 09:26