/*
* Copyright 2017 GcsSloop
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Last modified 2017-03-29 05:11:04
*
* GitHub: https://github.com/GcsSloop
* Website: http://www.gcssloop.com
* Weibo: http://weibo.com/GcsSloop
*/
package com.gcssloop.diycode.utils;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import java.util.List;
public class AppUtil {
/**
* 获取当前程序包名
*
* @param context 上下文
* @return 程序包名
*/
public static String getPackageName(Context context) {
return context.getPackageName();
}
/**
* 获取程序版本信息
*
* @param context 上下文
* @return 版本名称
*/
public static String getVersionName(Context context) {
String versionName = null;
String pkName = context.getPackageName();
try {
versionName = context.getPackageManager().getPackageInfo(pkName, 0).versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.e("VersionInfo", "Exception", e);
}
return versionName;
}
/**
* 获取程序版本号
*
* @param context 上下文
* @return 版本号
*/
public static int getVersionCode(Context context) {
int versionCode = -1;
String pkName = context.getPackageName();
try {
versionCode = context.getPackageManager().getPackageInfo(pkName, 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
Log.e("VersionInfo", "Exception", e);
}
return versionCode;
}
/**
* 判断是否安装某个应用
*
* @param context 上下文
* @param packageName 包名
* @return 是否安装
*/
public static boolean isAvailable(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();//获取packagemanager
List<PackageInfo> pInfo = packageManager.getInstalledPackages(0);//获取所有已安装程序的包信息
//从pinfo中将包名字逐一取出,压入pName list中
if (pInfo != null) {
for (int i = 0; i < pInfo.size(); i++) {
String pn = pInfo.get(i).packageName;
if (pn.equals(packageName))
return true;
}
}
return false;
}
}