公共代码参考(PackageManager)

package com.fredric.util;



import android.content.Context;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.content.pm.PackageManager.NameNotFoundException;



/*-

 * APP辅助类

 */



public class AppUtils {



    //获取APP的名称

    public static String getAppName(Context context){

            

        try {

            PackageManager packageManager = context.getPackageManager();

            PackageInfo packetinfo 

                = packageManager.getPackageInfo(context.getPackageName(), 0);

            //PackageInfo 对应整个Packet

            //ApplicationInfo主要是AndroidManifest.xml 文件中<Application/>标签对应的相应信息的封装

            //下例为:android:label="@string/app_name"

            int labelRes = packetinfo.applicationInfo.labelRes;

            return context.getResources().getString(labelRes);

            

        } catch (NameNotFoundException e) {

            e.printStackTrace();

        }

        

        return null;    

    }

    

    //获取版本名

    public static String getAppVersion(Context context){

        try {

            PackageManager packageManager = context.getPackageManager();

            PackageInfo packetinfo 

                = packageManager.getPackageInfo(context.getPackageName(), 0);

            return packetinfo.versionName;            

        } catch (NameNotFoundException e) {

            e.printStackTrace();

        }        

        return null;

    }

    

}

 

你可能感兴趣的:(package)