fwoop
fwoop

Reputation: 75

How to get the application name of the APK programmatically (not installed)

Based How to get the name of the application in android?.

I have this snippet. `andPath| is the location of the APK.

PackageInfo info = getPackageManager().getPackageArchiveInfo(andPath, 0);
ApplicationInfo appinfo = info.applicationInfo;
String packageName = getPackageManager().getApplicationLabel(appinfo).toString();

PackageManager pm=getPackageManager();
ApplicationInfo ai;
try {
     ai = pm.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {
     ai = null;
}

final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");

applicationName returns me the package name.

CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,PackageManager.GET_META_DATA));

c returns me null.

Does anyone know what I'm missing here?

Upvotes: 3

Views: 14354

Answers (5)

Alok Singh
Alok Singh

Reputation: 163

Simply use regular expression to get the name and compare with the app Label.

Upvotes: 0

Axay Prajapati
Axay Prajapati

Reputation: 811

String title = getResources().getString(R.string.app_name);

Upvotes: 0

Bucks
Bucks

Reputation: 687

public String applicationLabel (ApplicationInfo info){
     PackageManager p = con.getPackageManager();
     return p.getApplicationLabel(info).toString();
}

I hope you got application info pass that application info to this function it will give application name.

Upvotes: 0

Alex Lipov
Alex Lipov

Reputation: 13958

This is how you can get the application label from not yet installed APK file:

public static String getAppLabel(PackageManager pm, String pathToApk) {  
    PackageInfo packageInfo = pm.getPackageArchiveInfo(pathToApk, 0);

    if (Build.VERSION.SDK_INT >= 8) {
        // those two lines do the magic:
        packageInfo.applicationInfo.sourceDir = pathToApk;
        packageInfo.applicationInfo.publicSourceDir = pathToApk;
    }

    CharSequence label = pm.getApplicationLabel(packageInfo.applicationInfo);
    return label != null ? label.toString() : null;
}

No need to patch the applicationInfo prior to SDK 8. See this issue for more details.

Upvotes: 9

Bucks
Bucks

Reputation: 687

if you have a problem here i post the full code to get the application name

its returns ApplicationInfo

public List<ApplicationInfo> getApplicationList(Context con){
        PackageManager p = con.getPackageManager(); 
        List<ApplicationInfo> info = p.getInstalledApplications(0);
        return info;
    }

it returns ApplicationName

public String applicationLabel(Context con,ApplicationInfo info){
        PackageManager p = con.getPackageManager();
        String label = p.getApplicationLabel(info).toString();
        return label;
    }

on your onCreate function paste the below code

List<ApplicationInfo> info = new ArrayList<ApplicationInfo>();

        info = list.getApplicationList(this);
        LinearLayout layout = (LinearLayout)findViewById(R.id.content);
        layout.setOrientation(1);
        int size = info.size();
        Log.d("size",String.valueOf(size));
        for(int i=0;i<size;i++){
            Log.d("Val of I",String.valueOf(i));
            String appname = list.applicationLabel(this,info.get(i)).toString();
            Log.d("AppName",appname);
            TextView tv = new TextView(this);
            tv.setText(appname);
            layout.addView(tv);
        }

    }

Upvotes: 4

Related Questions