android 判斷App程序是否存在,處在前臺,後臺,前後臺切換,service是否執行,設定為系統app
阿新 • • 發佈:2018-12-17
1.判斷程序是否存在
//判斷是否在主程序,這個方法判斷程序名或者pid都可以,如果程序名一樣那pid肯定也一樣 //true:當前程序是主程序 false:當前程序不是主程序 public boolean isUIProcess() { ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE)); List<ActivityManager.RunningAppProcessInfo> 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<ActivityManager.RunningServiceInfo> 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<ActivityManager.RunningTaskInfo> 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();
}
}
}