|
楼主 |
发表于 2015-10-8 10:49:02
|
显示全部楼层
本帖最后由 xiaolonghun1 于 2015-10-8 10:55 编辑
/**
* 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;
}
System.out.println("installSlient filePath = " + filePath);
String[] args = { "pm", "install", "-r", filePath };
ProcessBuilder processBuilder = new ProcessBuilder(args);
System.out.println("installSlient 1");
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = new StringBuilder();
StringBuilder errorMsg = new StringBuilder();
int result;
try
{
process = processBuilder.start();
System.out.println("installSlient 2");
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);
System.out.println("successMsg:" + successMsg + ", ErrorMsg:" + errorMsg);
if(result==0){
boolean isSuccess = false;
String cmd = "am start -n " + "com.example.nxecoii" + "/" + "com.nxecoii.activity.MainActivity" + " \n";
System.out.println("run cmd :"+cmd);
try {
Process process1 = Runtime.getRuntime().exec(cmd);
isSuccess = waitForProcess(process1);
} catch (IOException e) {
// NLog.i(TAG, e.getMessage());
e.printStackTrace();
}
System.out.println("run the startApk------flage----:"+isSuccess);
}
return result;
}
1.之前的安装代码有点问题。我已经贴上来了。
2.就是静默安装主要有两个步骤,一个是安装apk包,还有就是启动你的apk应用。第二步是要在前一步执行完成了之后才会执行的。如果同一app的话,就会在安装第一步的时候,就把之前的app覆盖掉了,不会再继续执行第二步。而且要执行第二步的时候也会报上面之前贴出来的代码的那一部分错误。
3。恩,是用一个app更新另外为一个app。你可以弄成后台进行服务之类的,开机的时候把他启动起来。
|
|