android代码集锦
调用root权限的应用:
1 /** 2 * 执行Command命令的函数 3 * 4 * @param command 命令 5 * @return 执行结果 6 */ 7 public static boolean runRootCommand(String command) { 8 Process process = null; 9 DataOutputStream os = null; 10 try { 11 process = Runtime.getRuntime().exec("su"); 12 os = new DataOutputStream(process.getOutputStream()); 13 os.writeBytes(command + "\n"); 14 os.writeBytes("exit\n"); 15 os.flush(); 16 process.waitFor(); 17 } catch (Exception e) { 18 Log.d(TAG, "the device is not rooted, error message: " + e.getMessage()); 19 return false; 20 } finally { 21 try { 22 if (os != null) { 23 os.close(); 24 } 25 if (process != null) { 26 process.destroy(); 27 } 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 } 32 return true; 33 }
获取安装了所有APP的信息:
1 class PInfo { 2 private String appname = ""; 3 private String pname = ""; 4 private String versionName = ""; 5 private int versionCode = 0; 6 private Drawable icon; 7 8 private void prettyPrint() { 9 Log.i("MainActivity", appname + "|\t" + pname + "|\t" + versionName + "|\t" + versionCode + "|\t"); 10 } 11 } 12 13 private void listPackages() { 14 // false = no system packages 15 ArrayList<PInfo> apps = getInstalledApps(false); 16 final int max = apps.size(); 17 for (int i = 0; i < max; i++) { 18 apps.get(i).prettyPrint(); 19 } 20 } 21 22 private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) { 23 ArrayList<PInfo> res = new ArrayList<PInfo>(); 24 List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); 25 for (int i = 0; i < packs.size(); i++) { 26 PackageInfo p = packs.get(i); 27 if ((!getSysPackages) && (p.versionName == null)) { 28 continue; 29 } 30 PInfo newInfo = new PInfo(); 31 newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); 32 newInfo.pname = p.packageName; 33 newInfo.versionName = p.versionName; 34 newInfo.versionCode = p.versionCode; 35 newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); 36 res.add(newInfo); 37 } 38 return res; 39 }
==
作者:无言
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎微博互粉
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。