Android APK Information Get getNameApkInstalledFile(String packageName, int type)

Here you can find the source of getNameApkInstalledFile(String packageName, int type)

Description

get Name Apk Installed File

Declaration

@SuppressLint("SdCardPath")
    private static String getNameApkInstalledFile(String packageName,
            int type) throws IOException, InterruptedException 

Method Source Code

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;

public class Main{
    public static int USER_APP = 1;
    public static int SYSTEM_APP = 2;
    private static BufferedReader br;
    @SuppressLint("SdCardPath")
    private static String getNameApkInstalledFile(String packageName,
            int type) throws IOException, InterruptedException {
        String result = "";
        File tempFile = FileUtils.createNewFile("/sdcard/list_app.txt");

        String[] commands = null;
        if (type == USER_APP) {
            commands = new String[] {
                    "sysrw",
                    "ls -l /data/app > " + tempFile.getAbsolutePath(),
                    "ls -l /data/app-private >> "
                            + tempFile.getAbsolutePath(), "sysro" };
        } else if (type == SYSTEM_APP) {
            commands = new String[] { "sysrw",
                    "ls -l /system/app > " + tempFile.getAbsolutePath(),
                    "sysro" };
        }//from w  w  w.  j av a  2 s.c  o m
        runAsRoot(commands);

        String[] listPartPackageName = packageName.split("\\.");
        try {
            FileInputStream in = new FileInputStream(tempFile);
            br = new BufferedReader(new InputStreamReader(in));
            String strLine = "";
            boolean isApk = false;
            while ((strLine = br.readLine()) != null) {
                isApk = true;
                for (String s : listPartPackageName) {
                    if (!strLine.contains(s)) {
                        isApk = false;
                        break;
                    }
                }
                if (isApk) {
                    break;// End while loop
                }
            }
            if (strLine != "" && strLine != null) {
                String[] parts = strLine.split(" ");
                for (String part : parts) {
                    for (String s : listPartPackageName) {
                        result = part;
                        if (!strLine.contains(s)) {
                            result = "";
                            break;
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void runAsRoot(String[] commands) throws IOException,
            InterruptedException {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String cmd : commands) {
            os.writeBytes(cmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
        os.close();
        p.waitFor();
    }
}

Related

  1. getAppInfo(Context c, String name)
  2. getAppLabel(Context c, int uid)
  3. getAppLabel(Context c, int uid)
  4. getAppPkg(Context c, int uid)
  5. getAppPkg(Context c, int uid)
  6. isAppPlatformSignature(Context ctx)
  7. isApplicationBroughtToBackground(Context context)
  8. isApplicationSentToBackground( final Context context)
  9. restoreToUserApp(Context context)