EnthuDeveloper
EnthuDeveloper

Reputation: 670

Android application crash on getPackageManager

In my android application I need to get the version of the application programatically. But whenever I try with this code it crashes at this line. Is there anything that I am missing

PackageInfo pinfo = getPackageManager().getPackageInfo(
                    this.getPackageName(), 0);

I tried getting it from PackageManager and PackageInfo. In both the cases the application crashes and launches ViewRoot.handleMessage and I get a message "Source Not found"

Upvotes: 0

Views: 1890

Answers (1)

Chirag
Chirag

Reputation: 56925

Please try below function.

public int getVersion(Context context) 
{
        try 
        {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo("here.thenameofyour.package", PackageManager.GET_META_DATA);
            return pInfo.versionCode;
        } 
        catch (NameNotFoundException e) 
        {
            return 0;
        }
    }
}

Upvotes: 1

Related Questions