Android常用代码之普通及系统权限静默安装APK
2014-12-20 15:32
561 查看
http://www.trinea.cn/android/android-install-silent/
本文主要介绍程序如何安装apk,包括普通模式安装和系统权限静默安装。
如果是非系统应用请直接查看:Android常用代码之APK root权限静默安装,查看更完美的解决方案。
1、普通模式安装,调用系统Intent,代码如下:
Java
public static void install(Context context, String filePath) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
2、[b]静默安装[/b]
如果是非系统应用请移步:Android常用代码之APK root权限静默安装,查看更完美的解决方案。
分为如下三步
(1)、静默安装需要系统应用安装权限,需要在AndroidManifest.xml中添加
Java
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
(2)、实现代码
静默安装代码如下,实际是通过pm install -r 命令安装。
注意:该函数最好在新建的线程中运行并通过handler发送安装结果给主线程,否则安装时间较长会导致ANR。
静默安装实现
Java
/**
* install slient
*
* @param context
* @param filePath
* @return 0 means normal, 1 means file not exist, 2 means other exception error
*/
public static int installSlient(Context context, String filePath) {
File file = new File(filePath);
if (filePath == null || filePath.length() == 0 || (file = new File(filePath)) == null || file.length() <= 0
|| !file.exists() || !file.isFile()) {
return 1;
}
String[] args = { "pm", "install", "-r", filePath };
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
int result;
try {
process = processBuilder.start();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
} catch (IOException e) {
e.printStackTrace();
result = 2;
} catch (Exception e) {
e.printStackTrace();
result = 2;
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
// TODO should add memory is not enough here
if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {
result = 0;
} else {
result = 2;
}
Log.d("installSlient", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
return result;
}
返回值0表示安装成功,1表示文件不存在,2表示其他错误。需要更丰富的安装失败信息(内存不足、解析包出错)可直接使用PackageUtils.installSlient。
(3) 、获取系统权限
完成了上面两步后,普通方式安装我们的应用仍然无法静默安装。需要我们的应用获得系统权限,编译应用并通过
adb push <your_apk_path> /system/app/
命令实现安装,即可拥有系统权限。
本文主要介绍程序如何安装apk,包括普通模式安装和系统权限静默安装。
如果是非系统应用请直接查看:Android常用代码之APK root权限静默安装,查看更完美的解决方案。
1、普通模式安装,调用系统Intent,代码如下:
Java
public static void install(Context context, String filePath) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive");
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
1 2 3 4 5 6 | public static void install(Context context, String filePath) { Intent i = new Intent(Intent.ACTION_VIEW); i.setDataAndType(Uri.parse("file://" + filePath), "application/vnd.android.package-archive"); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } |
如果是非系统应用请移步:Android常用代码之APK root权限静默安装,查看更完美的解决方案。
分为如下三步
(1)、静默安装需要系统应用安装权限,需要在AndroidManifest.xml中添加
Java
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
1 | <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> |
静默安装代码如下,实际是通过pm install -r 命令安装。
注意:该函数最好在新建的线程中运行并通过handler发送安装结果给主线程,否则安装时间较长会导致ANR。
静默安装实现
Java
/**
* install slient
*
* @param context
* @param filePath
* @return 0 means normal, 1 means file not exist, 2 means other exception error
*/
public static int installSlient(Context context, String filePath) {
File file = new File(filePath);
if (filePath == null || filePath.length() == 0 || (file = new File(filePath)) == null || file.length() <= 0
|| !file.exists() || !file.isFile()) {
return 1;
}
String[] args = { "pm", "install", "-r", filePath };
ProcessBuilder processBuilder = new ProcessBuilder(args);
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
int result;
try {
process = processBuilder.start();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s);
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
} catch (IOException e) {
e.printStackTrace();
result = 2;
} catch (Exception e) {
e.printStackTrace();
result = 2;
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
// TODO should add memory is not enough here
if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) {
result = 0;
} else {
result = 2;
}
Log.d("installSlient", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
return result;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | /** * install slient * * @param context * @param filePath * @return 0 means normal, 1 means file not exist, 2 means other exception error */ public static int installSlient(Context context, String filePath) { File file = new File(filePath); if (filePath == null || filePath.length() == 0 || (file = new File(filePath)) == null || file.length() <= 0 || !file.exists() || !file.isFile()) { return 1; } String[] args = { "pm", "install", "-r", filePath }; ProcessBuilder processBuilder = new ProcessBuilder(args); Process process = null; BufferedReader successResult = null; BufferedReader errorResult = null; StringBuilder successMsg = new StringBuilder(); StringBuilder errorMsg = new StringBuilder(); int result; try { process = processBuilder.start(); successResult = new BufferedReader(new InputStreamReader(process.getInputStream())); errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream())); String s; while ((s = successResult.readLine()) != null) { successMsg.append(s); } while ((s = errorResult.readLine()) != null) { errorMsg.append(s); } } catch (IOException e) { e.printStackTrace(); result = 2; } catch (Exception e) { e.printStackTrace(); result = 2; } finally { try { if (successResult != null) { successResult.close(); } if (errorResult != null) { errorResult.close(); } } catch (IOException e) { e.printStackTrace(); } if (process != null) { process.destroy(); } } // TODO should add memory is not enough here if (successMsg.toString().contains("Success") || successMsg.toString().contains("success")) { result = 0; } else { result = 2; } Log.d("installSlient", "successMsg:" + successMsg + ", ErrorMsg:" + errorMsg); return result; } |
(3) 、获取系统权限
完成了上面两步后,普通方式安装我们的应用仍然无法静默安装。需要我们的应用获得系统权限,编译应用并通过
adb push <your_apk_path> /system/app/
命令实现安装,即可拥有系统权限。
相关文章推荐
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- Android常用代码之普通及系统权限静默安装APK
- 让Android程序获得系统的权限,实现关机重启,静默安装等功能
- android 4.0.4系统下实现apk的静默安装和启动
- [Unity]Android(安卓)代码安卓APK时,使用系统安装器安装的[最正确]办法(不弹出选择打开的程序)
- 让Android程序获得系统的权限,实现关机重启,静默安装等功能
- 让Android程序获得系统的权限,实现关机重启,静默安装等功能
- android 4.0.4系统下实现apk的静默安装和启动
- android 4.0.4系统下实现apk的静默安装和启动
- Android 4.0.4系统下实现apk的静默安装和启动
- Android 使用系统限制的权限的apk应安装到 /system/app 目录下
- ***android 4.0.4系统下实现apk的静默安装和启动
- android 4.0.4系统下实现apk的静默安装和启动
- Android-几行代码通知系统自动安装apk