Mehmet Ethem SULAN :: Android :: Read Package information from Manifest in Android

AndroidManifest.xml içinde tanımlanan android:versionCode, android:versionName ve paketimizin ismini package com.information.manifest; texviewe yazmaya çalıştım.
Manifest içindeki değerlere

getPackageManager().getPackageInfo() 

hazır metodlarla erişiyoruz.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.information.manifest"
      android:versionCode="2"
      android:versionName="1.1.4">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ManifestInformationActivity"
                  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>

ManifestInformationActivity.java

package com.information.manifest;

import android.app.Activity;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class ManifestInformationActivity extends Activity {
    private String vName, pName;
    private TextView textView_info;
	int vCode;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView_info=(TextView) findViewById(R.id.textView1_info);
        try {
//AndroidManifest.xml içinde tanımlanan versionCode, versionName ve paket ismini textview yazdim.
			vName=getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
			vCode=getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
			pName=getPackageManager().getPackageInfo(getPackageName(), 0).packageName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		textView_info.setText("Version Name: "+vName+"  Version Code: "+vCode+"  Package Name:"+pName);
    }
}

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:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView1_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView"></TextView>
</LinearLayout>

Download ManifestInformation.tar.gz

Write a Comment

Let me know what you think?