Wednesday, October 24, 2012

ApplicationInfo Android Example


ApplicationInfo Android Example
ApplicationInfo class provide particular application Information. It retrive all information from  the  manifest file .
For example you can get application package name and application version , os version and etc..

Get Version Name;
This method provide Particular  application version name.
 /**
     * @author vijayakumar
     * @return
     */
    public  String getApplicationVersion()
    {
        try
        {
            PackageManager pm = AndroidMADQAActivity.this.getPackageManager();
            return pm.getPackageInfo(AndroidMADQAActivity.this.getPackageName(),0).versionName;
        }
        catch (NameNotFoundException e)
        {
            return "";
        }
    }

Get PackageName
This Method provide particular application Packagename.

 public  CharSequence getApplicationPakagename()
    {
        try
        {
            PackageManager pm = AndroidMADQAActivity.this.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(AndroidMADQAActivity.this.getPackageName(),0);
            return pm.getApplicationLabel(ai);
        }
        catch (NameNotFoundException e)
        {
            return "non";
        }
    }

Get OS Version
This Method Provide  OS version.
  /**
     * @author vijayakumar
     * @return
     */
    public  String getOSVersion()
    {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.CUR_DEVELOPMENT)
        {
            return "DEVELOPMENT";
        }
        return Build.VERSION.RELEASE;
    }

Get DeviceModel
This Method provide devicemodel.
  /**
     * @author vijayakumar
     * @return
     */
    public  String getDeviceModel()
    {
        StringBuilder deviceModel = new StringBuilder();

        if ("sdk".equals(Build.MODEL) && "sdk".equals(Build.PRODUCT))
        {
            return "SDK Emulator";
        }
        deviceModel.append(Build.MODEL).append(" [");
        deviceModel.append(Build.MANUFACTURER).append(" ");
        deviceModel.append(Build.PRODUCT).append("]");
        return deviceModel.toString();
    }

ScreenShot



CompleteSoureCode

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.ViewFlipper;
/**
 *
 * @author vijayakumar
 *
 */
public class AndroidMADQAActivity extends Activity  {
     TextView textView = null;
     Activity act;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textView = (TextView)findViewById(R.id.text);
          textView.setText("Application Pakagename : " +getApplicationPakagename().toString()+"\n"+"*******************"+"\n"+"Application Version ::"+getApplicationVersion() +"\n"+"*******************"+"\n"+"Device Model:"+getDeviceModel()+"\n"+"*******************"+"\n"+"OS Version ::"+getOSVersion() );
      
    }
    public  CharSequence getApplicationPakagename()
    {
        try
        {
            PackageManager pm = AndroidMADQAActivity.this.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(AndroidMADQAActivity.this.getPackageName(),0);
            return pm.getApplicationLabel(ai);
        }
        catch (NameNotFoundException e)
        {
            return "non";
        }
    }
    /**
     * @author vijayakumar
     * @return
     */
    public  String getApplicationVersion()
    {
        try
        {
            PackageManager pm = AndroidMADQAActivity.this.getPackageManager();
            return pm.getPackageInfo(AndroidMADQAActivity.this.getPackageName(),0).versionName;
        }
        catch (NameNotFoundException e)
        {
            return "";
        }
    }
    /**
     * @author vijayakumar
     * @return
     */
    public  String getDeviceModel()
    {
        StringBuilder deviceModel = new StringBuilder();

        if ("sdk".equals(Build.MODEL) && "sdk".equals(Build.PRODUCT))
        {
            return "SDK Emulator";
        }
        deviceModel.append(Build.MODEL).append(" [");
        deviceModel.append(Build.MANUFACTURER).append(" ");
        deviceModel.append(Build.PRODUCT).append("]");
        return deviceModel.toString();
    }
    /**
     * @author vijayakumar
     * @return
     */
    public  String getOSVersion()
    {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.CUR_DEVELOPMENT)
        {
            return "DEVELOPMENT";
        }
        return Build.VERSION.RELEASE;
    }

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...