在开发中可能会遇到 获取app的版本号、渠道号。因此简单整理一下。
源码下载:http://download.csdn.net/detail/gl_mine_csdn/9756756点击打开链接
class ChannelUtils {
private static final String CHANNEL_KEY = "";//默认的通道
private static final String CHANNEL_VERSION_KEY = "";//默认的通道的版本号
private static String mChannel;//channel
private static String mDefaultChannel = "qihoo";
/**
* 返回市场。 如果获取失败返回""
*
* @param context
* @return
*/
public static String getChannel(Context context) {
//#1、使用第三方工具
// return WalleChannelReader.getChannel(context.getApplicationContext());
//由于360要打加固包,之后META-INF数据全部被改,所以取默认channel
//#2、自己创建工具类
return getChannel(context, mDefaultChannel);
}
/**
* 返回市场。 如果获取失败返回defaultChannel
*
* @param context
* @param defaultChannel
* @return
*/
private static String getChannel(Context context, String defaultChannel) {
//1、内存中获取
if (!TextUtils.isEmpty(mChannel)) {
return mChannel;
}
//2、sp中获取
mChannel = getChannelBySharedPreferences(context);
if (!TextUtils.isEmpty(mChannel)) {
return mChannel;
}
//3、从apk中获取
mChannel = getChannelFromApk(context, CHANNEL_KEY);
if (!TextUtils.isEmpty(mChannel)) {
//保存sp中备用
saveChannelBySharedPreferences(context, mChannel);
return mChannel;
}
//全部获取失败
return defaultChannel;
}
/**
* 从apk中获取版本信息
*
* @param context
* @param channelKey
* @return
*/
private static String getChannelFromApk(Context context, String channelKey) {
//从apk包中获取
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
//默认放在meta-inf/里, 所以需要再拼接一下
String key = "META-INF/" + channelKey;
String ret = "";
ZipFile zipfile = null;
try {
zipfile = new ZipFile(sourceDir);//获取压缩包文件
Enumeration<?> entries = zipfile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.startsWith(key)) {
ret = entryName;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] split = ret.split("_");
String channel = "";
if (split != null && split.length >= 3) {//此处需要和app本身的包名有关
channel = split[2];//获取channel名
//channel = ret.substring(split[0].length() + 1);
}
return channel;
}
/**
* 本地保存channel & 对应版本号
*
* @param context
* @param channel
*/
private static void saveChannelBySharedPreferences(Context context, String channel) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sp.edit();
editor.putString(CHANNEL_KEY, channel);
editor.putInt(CHANNEL_VERSION_KEY, getVersionCode(context));
editor.apply();
}
/**
* sp---->获取channel
*
* @param context
* @return 为空表示获取异常、sp中的值已经失效、sp中没有此值
*/
private static String getChannelBySharedPreferences(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
int currentVersionCode = getVersionCode(context);
if (currentVersionCode == -1) {
//获取错误
return "";
}
int versionCodeSaved = sp.getInt(CHANNEL_VERSION_KEY, -1);
if (versionCodeSaved == -1) {
//本地没有存储的channel对应的版本号
//第一次使用 或者 原先存储版本号异常
return "";
}
if (currentVersionCode != versionCodeSaved) {
return "";
}
return sp.getString(CHANNEL_KEY, "");
}
/**
* 包信息----->获取版本号
*
* @param context
* @return
*/
private static int getVersionCode(Context context) {
try {
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return -1;
}
}