关于静默安装和安装后自启动

来源:互联网 发布:for循环遍历json 编辑:程序博客网 时间:2024/05/21 10:07

静默安装的方法,其中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;@Overridepublic 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","安装插件----->");}@Overridepublic void onReceive(final Context context, Intent intent) {// TODO Auto-generated method stubsuper.onReceive(context, intent);cont=context.getApplicationContext();Log.i("TAG","接收到广播更新广播----->"+intent.getAction());if(h==null){h=new Handler();}if(r==null){r=new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {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 exceptione.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<RunningTaskInfo> 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插件一旦添加到桌面之后,会一直处于运行状态,即使开机重启,依然会继续运行。

实现静默安装并自动启动的步骤如下:

首先安装插件,安装需要静默安装更新的软件,每次检测到新的版本时,首先更新插件,然后更新软件,插件在更新后,自动调用onReceive方法,此时使用报名,打开自动更新完成的软件。

注:widget更新时,覆盖原来版本,无论新版本做不做改动都会调用onUpdate和onReceive方法

如有疑问,请联系我QQ2287989724