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     }

 

==

posted @ 2016-01-21 15:41  HuijunZhang  阅读(326)  评论(0编辑  收藏  举报
中国