用程序安装apk

本文介绍了一个安卓应用从下载到安装的全过程,包括如何保存下载的APK文件、更改文件权限以便安装,以及如何通过Intent启动安装流程。同时,文中还提供了一个检查指定包名的应用是否已安装的方法。

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

/**
* 改变文件权限
*/
public void changeModAndInstall() {


File destFile = new File(downloadpath + apkName);
String command = "chmod 777 " + destFile.getAbsolutePath();


Runtime runtime = Runtime.getRuntime();
try {
Process proc = runtime.exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


// 安装


Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
File f = new File(downloadpath + apkName);
if (f.exists()) {
intent.setDataAndType(Uri.fromFile(f),
"application/vnd.android.package-archive");
startActivity(intent);
}


}


/**

* @param path
*            下载后保存apk的路径
* @param apkName
*            apk的名称
* @param data
*            下载的数据
*/
public void saveAPK(String path, String apkName, byte[] data) {


String installPath = path + apkName;


File file = new File(installPath);


if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {


FileOutputStream fis = new FileOutputStream(file);


fis.write(data);
fis.close();
Log.i("info", "保存:" + installPath + data.length);
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 读取流信息
*/
public static byte[] read(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}


/**
* 检查 本地是否安装了   被调起的软件 (鑫财通)
* @param packageName
* @return
*/
public boolean checkApkExist(String packageName) {


try {
ApplicationInfo info = this.getPackageManager().getApplicationInfo(
packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}


@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值