Loading

Friday, July 9, 2010

About Me: PackageInfo.versionName and PackageInfo.versionCode

PackageManager is a class for retrieving various kinds of information related to the application packages that are currently installed on the device. You can find this class through getPackageManager(). And PackageInfo hold the overall information about the contents of a package. This corresponds to all of the information collected from AndroidManifest.xml.

About Me: PackageInfo.versionName and PackageInfo.versionCode

The information of android:versionCode and android:versionName in AndroidManifest.xml can be retrieved from PackageInfo.versionName and PackageInfo.versionCode.

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/hello"
/>
<LinearLayout
android:orientation="vertical"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/aboutme"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="- About Me -"
/>
</LinearLayout>
</LinearLayout>


AndroidAboutMe.java
package com.exercise.AndroidAboutMe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AndroidAboutMe extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonAboutMe = (Button)findViewById(R.id.aboutme);

buttonAboutMe.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String strVersion;

PackageInfo packageInfo;
try {
packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
strVersion = "Version Name: " + packageInfo.versionName +"\n"
+ "Version Code: " + String.valueOf(packageInfo.versionCode);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
strVersion = "Cannot load Version!";
}

new AlertDialog.Builder(AndroidAboutMe.this)
.setTitle("About Me!").setMessage(strVersion)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {}
}).show();
}});
}
}


Download the files.

SHARE TWEET

Thank you for reading this article About Me: PackageInfo.versionName and PackageInfo.versionCode With URL https://x-tutorials.blogspot.com/2010/07/about-me-packageinfoversionname-and.html. Also a time to read the other articles.

0 comments:

Write your comment for this article About Me: PackageInfo.versionName and PackageInfo.versionCode above!