静默安装的方法,其中packagePath为安装包的路径名
实现静默安装必须获取系统root权限
String cmd = "pm install -r "+packagePath;
Process process = null;
DataOutputStream os = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
try {
//静默安装需要root权限
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.write(cmd.getBytes());
os.writeBytes("\n");
os.writeBytes("exit\n");
os.flush();
//执行命令
process.waitFor();
//获取返回结果
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
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 (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (process != null) {
process.destroy();
}
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//显示结果
静默安装实现更新之后,并不会自动启动,所以需要编写一个插件来管理
在安装程序之前,需要首先安装一个插件,插件用widget的形式来编写,
widget的receiver并不能监听到系统安装程序的广播,所以在安装之前首先安装插件,
因为插件在更新时会调用onUpdate和onReceive方法,插件的代码如下
package com.example.widgetdemo;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.util.Log;
public class MyAppWidgetReceiver extends AppWidgetProvider {
private Handler h;
private Runnable r;
private Context cont;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
Intent intent = new Intent(context.getApplicationContext(),WidgetService.class);
context.getApplicationContext().startService(intent);
Log.i("TAG","安装插件----->");
}
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
cont=context.getApplicationContext();
Log.i("TAG","接收到广播更新广播----->"+intent.getAction());
if(h==null){
h=new Handler();
}
if(r==null){
r=new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
if(checkApkExist(cont, "com.example.testslientinstall")&&!checkIsRunning()){
PackageManager manager = context.getApplicationContext().getPackageManager();
Intent openQQ = manager.getLaunchIntentForPackage("com.example.testslientinstall");
context.getApplicationContext().startActivity(openQQ);
}else if(!checkApkExist(cont, "com.example.testslientinstall")){
h.postDelayed(r,1000*10);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
h.postDelayed(r, 5000);
}
}
};
}
h.removeCallbacks(r);
h.postDelayed(r,5*1000);
}
//检测应用是否在运行中
public boolean checkIsRunning(){
ActivityManager am = (ActivityManager)cont.getSystemService(Context.ACTIVITY_SERVICE);
List list = am.getRunningTasks(100);
boolean isAppRunning = false;
String MY_PKG_NAME = "com.example.testslientinstall";
for (RunningTaskInfo info : list) {
if (info.topActivity.getPackageName().equals(MY_PKG_NAME) || info.baseActivity.getPackageName().equals(MY_PKG_NAME)) {
isAppRunning = true;
Log.i("TAG",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
Log.i("TAG","正在运行中……");
break;
}
}
return isAppRunning;
}
//检测应用是否存在
public boolean checkApkExist(Context context, String packageName) {
if (packageName == null ||"".equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
Log.i("TAG","应用存在");
return true;
} catch (NameNotFoundException e) {
Log.i("TAG","应用不存在");
return false;
}
}
}
widget插件一旦添加到桌面之后,会一直处于运行状态,即使开机重启,依然会继续运行。
实现静默安装并自动启动的步骤如下:
首先安装插件,安装需要静默安装更新的软件,每次检测到新的版本时,自动下载安装,插件监听到新的软件安装后,启动新安装的软件。
注:widget更新时,覆盖原来版本,无论新版本做不做改动都会调用onUpdate和onReceive方法
在有的安卓版本中,不能实现安装自身,需要在插件实现软件的安装更新,并启动
如有疑问,请联系我QQ2287989724