How to search the path of an app by its name?

3

I want to search the path of an app by its name, I searched and researched and the PackageManager method seems the ideal but I do not know how to adapt it so that it does what is desired.

    
asked by PowerApp 19.10.2016 в 18:53
source

1 answer

2

This is a method that I create, which through the name of the application you get the data directory of the application.

private String getPathFromAppname(String appName){
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    String applicationPath = "";
    final List<PackageInfo> listadoPackages =  getPackageManager().getInstalledPackages(0);
    for (int i=0; i < listadoPackages.size(); i++)
    {
        PackageInfo packInfo = listadoPackages.get(i);
        if (  (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
        {
            if(appName.equals(packInfo.applicationInfo.loadLabel(getPackageManager()).toString())) {
                //String nombreApplicacion = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
                //Log.i("Nombre de aplicación: " + Integer.toString(i), nombreApplicacion);
                //String paqueteApplicacion = packInfo.packageName;
                Log.i("packagename: " + Integer.toString(i), paqueteApplicacion);
                applicationPath = packInfo.applicationInfo.dataDir;
                Log.i("sourceDir: " + Integer.toString(i), packInfo.applicationInfo.sourceDir);
            }
        }
    }
    return applicationPath;
}

An example of how to call it:

Log.i("Path: ", getPathFromAppname("[Nombre de Aplicación]"));

It is important to mention that you can have several applications with the same name installed on your device, so the correct thing to do is to use the PackageName (alias id of application) which is unique, instead the names displayed by application NO.

    
answered by 20.10.2016 / 02:12
source