download something

本文介绍了如何在Android设备上获取已安装应用的版本信息、读取指定APK的版本号、查询存储空间剩余情况、联网下载文件及安装指定的APK文件等关键技术实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
}

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值