对APK进行覆盖安装和卸载

public class PackageManager {
private Context mContext;
private String apkName = "";


public PackageManager(Context mContext, String apkName) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
this.apkName = apkName;
}


/**安装

* @param apkName
*            如xxx.apk
*/
public void installApk(/*InputStream is*/) {
String apkPathName =Environment.getExternalStorageDirectory()+ "/kuaixindai/"+ apkName;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String type = "application/vnd.android.package-archive";
try {
// //不知道什么原因,当apk大于5M的时候就读取异常
File f = new File(apkPathName);
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = mContext.openFileOutput(apkName,
Context.MODE_PRIVATE + Context.MODE_WORLD_READABLE);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.flush();
fis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
File f = new File(apkPathName);
if (f.exists()) {
intent.setDataAndType(Uri.fromFile(f), type);
mContext.startActivity(intent);
}
}

       //卸载
public void uninstallApk(String packageName) {
Uri packageURI = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
mContext.startActivity(uninstallIntent);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值