特此声明本文转载
静默安装与系统安装的区别:
一种 发给系统 让系统安装 但是会出现 安装界面 还得点击 比较麻烦。
一种 是 静默安装, 不了解的 百度一下就出来了。 这个安装后, 如果手机安装了 360 百度管家 等软家 在通知栏会提示 软件安装成功。<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CLEAR_APP_USER_DATA" />
+权限 因为有些是系统权限 +进去后 manifest 会出错 clean 一下 就可以了 。
要安装的 apk 放到 assets 目录下 安装时 先将 目录下的 apk 拷贝到 SD 卡上 改名为 temp.apk 然后安装temp.apk
直接发代码// 判断是否已安装
PackageInfo packageInfo;
try {
packageInfo = getPackageManager().getPackageInfo(
"com.example.imgrefocus", 0);
} catch (NameNotFoundException e) {
packageInfo = null;
e.printStackTrace();
}
if (packageInfo == null) {
// 启用安装新线程
new Thread(new Runnable() {
@Override
public void run() {
Log.e("hao", "未安装进行安装");
slientInstall(); // 未安装进行安装
}
}).start();
} else {
Log.e("hao", "已经安装");
}
通过包名 先判断要安装的 apk 是否 存在 不存在再开线程 安装
###################################################################################################
/**
* 静默安装
*
* @param file
* @return
*/
public boolean slientInstall() {
createFile(); // 进行资源的转移 将assets下的文件转移到可读写文件目录下
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/temp.apk");
boolean result = false;
Process process = null;
OutputStream out = null;
System.out.println(file.getPath());
if (file.exists()) {
System.out.println(file.getPath() + "==");
try {
process = Runtime.getRuntime().exec("su");
out = process.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(out);
dataOutputStream.writeBytes("chmod 777 " + file.getPath()
+ "\n"); // 获取文件所有权限
dataOutputStream
.writeBytes("LD_LIBRARY_PATH=/vendor/lib:/system/lib pm install -r "
+ file.getPath()); // 进行静默安装命令
// 提交命令
dataOutputStream.flush();
// 关闭流操作
dataOutputStream.close();
out.close();
int value = process.waitFor();
// 代表成功
if (value == 0) {
Log.e("hao", "安装成功!");
result = true;
} else if (value == 1) { // 失败
Log.e("hao", "安装失败!");
result = false;
} else { // 未知情况
Log.e("hao", "未知情况!");
result = false;
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!result) {
Log.e("hao", "root权限获取失败,将进行普通安装");
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
startActivity(intent);
result = true;
}
}
return result;
}
public void createFile() {
InputStream is = null;
FileOutputStream fos = null;
try {
is = MainActivity.this.getAssets().open("ImgRefocus.apk");
File file = new File(Environment.getExternalStorageDirectory()
.getPath() + "/temp.apk");
file.createNewFile();
fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}