将安卓的两个APk打包在一个pak中
在开发的过程中,遇到安卓应用的保活问题,解决的办法是安装两个apk,使用AIDL通信方式互相保持联系,发现一方死亡立即拉起,从而打到保活的效果,但是问题来了,你给客户安装也不能让他一下安装两个apk吧。
最后在网上找到了一个合理的解决办法是,讲一个apk更名为.mp3格式放到以资源文件的形式放到第二个项目中,放在assets目录下,然后安装启动apk,将.mp3转换为apk安装到手机上。
注意:必须知道要转换成.mp3格式的apk包名才可使用安装,例:com.example.live1。核心代码如下
//将live1安装在手机上
private void install(Context context){
try {
//建立live1的apk路径
String path=context.getCacheDir().getAbsoluteFile()+"/live1.apk";
File file=new File(path);
//判断文件路径是否存,不存在就新建一个。
if (!file.exists()){
file.createNewFile();
}
//获取本地的live1.mp3,mp3只是将live1的扩展名修改了一下,原来是以.apk结尾的。
InputStream is=context.getClass().getClassLoader().getResourceAsStream("assets/live1.mp3");
//将live.mp3的内容写入到live1.apk中。
inputStreamToFile(is, file);
//提升路径
String cmd = "chmod 777 " + file.getAbsolutePath();
Runtime.getRuntime().exec(cmd);
cmd="chmod 777 " +file.getParent();
Runtime.getRuntime().exec(cmd);
cmd="chmod 777"+new File(file.getParent()).getParent();
Runtime.getRuntime().exec(cmd);
//开始安装
Intent intent=new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
/* 设置intent的file与MimeType */
intent.setDataAndType(Uri.fromFile(file), type);
context.startActivity(intent);
}catch (Exception e){
Log.d("错误:",e.getMessage());
}
}