I've been playing around with android.R.attr.versionName, which I first saw on this question. This claims to show the version of the app, but I've tried a couple of things that just don't seem to work:

  1. View as a TextView, just shows number:

    <TextView ...
         android:text="?android:attr/versionName"/>
    
  2. Get as a string, returns a null pointer exception:

    TypedValue value=new TypedValue();
    getActivity().getTheme().resolveAttribute(android.R.attr.versionName,value,true);
    String version=value.coerceToString().toString();
    

Any other thoughts? It seems rather useless if there isn't a way to get to the version from either code or XML.

I do know that there are other ways to get the version name, and I'm not interested in them, I'm just curious about this specific variable, and what it's use is.

有帮助吗?

解决方案

but I've tried a couple of things that just don't seem to work

I would not expect either of those two to work. A View is not a manifest file. A layout is not a manifest file.

It seems rather useless if there isn't a way to get to the version from either code or XML.

It is simply not used for processing views. Few things in android.R.attr pertain to views. At most, this value might be used when Android internally parses the manifest. My guess is that it is in android.R.attr as a side-effect of some other metadata declaration, as a quick casual search of the Android source code did not turn up a match.

其他提示

Below is a simple class that you can use. It's coded to deal with the deprecated packageInfo.versionCode in Android SDK 28, and it suppresses the deprecation lint error.

public class Version  {

    private Activity activity;

    public Version(Activity activity) {
        this.activity = activity;
    }

    @SuppressWarnings("deprecation")
    public long getCode() {
        long code = 0;
        try {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
                code = activity.
                        getPackageManager().getPackageInfo
                        (activity.getPackageName(), 0).getLongVersionCode();
            } else{
                code = activity.
                    getPackageManager().getPackageInfo
                    (activity.getPackageName(), 0).versionCode;
            }
        } catch (PackageManager.NameNotFoundException e) {
        }
        return code;
    }

    public String getName() {
        String name = "";
        try {
            name = activity.
                    getPackageManager().getPackageInfo
                     (activity.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
        }
        return name;
    }
}

Try this:

    TextView yourtextview = 
    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_META_DATA);
    yourtextview.setText(pInfo.versionName));

or as you can see on the page you have linked this:

    android:text="?android:attr/versionName"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top