1、得到系统安装的程序的version
String version = getPackageManager().getPackageInfo("com.gotsun.tongbulianxi", 0).versionName;
2、得到指定apk的Version
private String getUninstallAPKInfo(Context ctx,String archiveFilePath) {
//archiveFilePath=Environment.getExternalStorageDirectory()+"/"+"TestB.apk"
String versionName = null;
String appName = null;
String pakName = null;
PackageManager pm=ctx.getPackageManager();
PackageInfo pakinfo=pm.getPackageArchiveInfo(archiveFilePath,PackageManager.GET_ACTIVITIES);
if (pakinfo!=null) {
ApplicationInfo appinfo=pakinfo.applicationInfo;
versionName=pakinfo.versionName;
Drawable icon=pm.getApplicationIcon(appinfo);
appName=(String) pm.getApplicationLabel(appinfo);
pakName=appinfo.packageName;
}
return versionName;
}
3、获得指定路径的剩余空间
public static long getAvailableStore(String filePath)
{
// 取得sdcard文件路径
StatFs statFs = new StatFs(filePath);
// 获取block的SIZE
long blockSize = statFs.getBlockSize();
// 获取BLOCK数量
// long totalBlocks = statFs.getBlockCount();
// 可使用的Block的数量
long availaBlock = statFs.getAvailableBlocks();
// long total = totalBlocks * blocSize;
long availableSpare = availaBlock * blockSize;
return availableSpare;
// return availableSpare / 1024 / 1024;
}
4、联网下载
URL url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
int length = httpURLConnection.getContentLength();
System.out.println("selfApkSize---------------->" + length);
if (httpURLConnection.getResponseCode() == 200)
{
InputStream is = httpURLConnection.getInputStream();
byte[] buffer = new byte[1024 * 5];
int len = 0;
FileOutputStream fos = new FileOutputStream(sdFile);
while ((len = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len);
sum += len;
System.out.println("sum---------->" + sum);
int pre = (int) (sum / (double) length * 100);
if (pre - precent >= 1)
{
precent = pre;
msg = new Message();
msg.what = MSG_DOWNLOAD_RESULT;
msg.arg1 = precent;
handler.sendMessage(msg);
}
}
is.close();
fos.flush();
fos.close();
} else
{
}
5、安装指定的apk文件
protected void install(String tempPath)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(tempPath)),"application/vnd.android.package-archive");
startActivity(intent);
}