获取应用程序的信息

1、获取应用程序名

PackageManager pm = context.getPackageManager();

ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), 0);

CharSequence cs = pm.getApplictionLabel(ai);


2、获取应用程序的版本号:

PackageManager pm = context.getPackageManager();

String version = pm.getPackageInfo(context.getPackageName(), 0).versionName;


3、获取os版本

Build.VERSION.SDK_INT其对应的值可能存在Build.VERSION_CODES

String osVersion = Build.VERSION.RELEASE;


package com.example.androidtest;

import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;

public final class AndroidInfo
{
    private static final String EMULATOR_ID = "ffffffffffffffff";

    public static CharSequence getApplicationLabel(Context context)
    {
        try
        {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(),0);
            return pm.getApplicationLabel(ai);
        }
        catch (NameNotFoundException e)
        {
            return "AnonDroid";
        }
    }

    public static String getApplicationVersion(Context context)
    {
        try
        {
            PackageManager pm = context.getPackageManager();
            return pm.getPackageInfo(context.getPackageName(),0).versionName;
        }
        catch (NameNotFoundException e)
        {
            return "";
        }
    }

    public static boolean isOnEmulator(Context context)
    {
        if ("sdk".equals(Build.MODEL) && "sdk".equals(Build.PRODUCT))
        {
            return true;
        }

        return getUniqueDeviceID(context).equals(EMULATOR_ID);
    }

    public static String getDeviceModel()
    {
        StringBuilder ret = new StringBuilder();

        if ("sdk".equals(Build.MODEL) && "sdk".equals(Build.PRODUCT))
        {
            return "SDK Emulator";
        }

        ret.append(Build.MODEL).append(" [");
        ret.append(Build.MANUFACTURER).append(" ");
        ret.append(Build.PRODUCT).append("]");

        return ret.toString();
    }

    public static String getOSVersion()
    {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.CUR_DEVELOPMENT)
        {
            return "DEV";
        }
        return Build.VERSION.RELEASE;
    }

    public static String getUniqueDeviceID(Context context)
    {
        ContentResolver contentResolver = context.getContentResolver();
        String id = android.provider.Settings.System.getString(contentResolver,android.provider.Settings.System.ANDROID_ID);
        if (id == null)
        {
            id = EMULATOR_ID; // running on emulator.
        }
        return id;
    }

    /**
     * Build an HTTP User-Agent suitable enough to identify this application + version + handset
     */
    public static String getUserAgent(Context context)
    {
        StringBuilder ua = new StringBuilder();
        ua.append(AndroidInfo.getApplicationLabel(context)).append("/");
        ua.append(AndroidInfo.getApplicationVersion(context));
        ua.append(" (Android ").append(AndroidInfo.getOSVersion());
        ua.append("/").append(AndroidInfo.getDeviceModel()).append(")");
        return ua.toString();
    }

    private AndroidInfo()
    {
        /* prevent instantiation */
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值