Android: read application version
May 09, 2011 14:15:01 Last update: May 09, 2011 14:17:22
In short, use the PackageManager class to get the
To build an APK, make these changes to the greetings app:
PackageInfo
:
PackageInfo pkgInfo = getPackageManager().getPackageInfo(name.toString(), 0); Log.d("TEST", "Version code: " + pkgInfo.versionCode); Log.d("TEST", "Version name: " + pkgInfo.versionName);
To build an APK, make these changes to the greetings app:
- Change the layout to (
res/layout/main.xml
):<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/prompt" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/name" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/getversion" android:onClick="getVersion" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/versionInfo" /> </LinearLayout>
- Change the string values (
res/values/strings.xml
):<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Get Application Version</string> <string name="prompt">Enter Package Name:</string> <string name="getVersion">Get Version</string> <string name="packageNotFound">Package not found.</string> <string name="packageInfo">Version code: %s, version name: %s</string> </resources>
- Update
AndroidManifest.xml
:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.appinfo" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name" android:icon="@drawable/icon"> <activity android:name="GetAppVersion" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
- Create a new Java class
src/com/android/appinfo/GetAppVersion.java
:package com.android.appinfo; import android.app.Activity; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.Signature; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class GetAppVersion extends Activity { private static final String LOG_TAG = "GetAppVersion"; private EditText nameField; private TextView versionInfoField; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nameField = (EditText) findViewById(R.id.name); versionInfoField = (TextView) findViewById(R.id.versionInfo); } public void getVersion(View view) { CharSequence name = nameField.getText(); try { PackageInfo pkgInfo = getPackageManager().getPackageInfo(name.toString(), 0); versionInfoField.setText(String.format( getString(R.string.packageInfo), pkgInfo.versionCode, pkgInfo.versionName )); } catch (PackageManager.NameNotFoundException e) { versionInfoField.setText(getString(R.string.packageNotFound)); } } }
- Edit
build.xml
, change project name to "AppInfo":<?xml version="1.0" encoding="UTF-8"?> <project name="AppInfo" default="help"> . . . </project>
- Install to emulator:
ant install
- Screenshot: