关于静默安装和安装后自启动
来源:互联网 发布: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
阅读全文
0 0
- 关于静默安装和安装后自启动
- android 静默安装之后自启动
- rpm打包和yum安装,以及安装后自启动
- 静默安装和静默卸载
- android 版本更新 静默安装及自启动
- 关于静默安装apk
- 安装oracle后,配置监听和oracle自启动
- android实现静默下载、静默安装、获取root权限并自动安装成系统APP、自启动
- Android静默安装和静默卸载
- Android--静默安装和静默卸载
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- android实现静默卸载和静默安装
- Android静默安装和静默卸载
- Android静默安装和静默卸载
- symbian 3rd 安装后自启动
- LNMP安装后自启动设置
- App安装未运行后自启动
- 对于初学者一篇非常形象的进程与线程解读
- C#语法:委托与方法
- nginx常用配置
- JavaScript_函数
- JUnit
- 关于静默安装和安装后自启动
- RobotFramework之Dialogs
- LA 4254 二分
- 机器学习聚类算法
- 有名的面试题 有一个二维数组(n*n)
- 按照SCN 闪回数据库!!ORA-38754: FLASHBACK DATABASE not started; required redo log is not available
- 1.jdk认识及使用
- SpringBoot:使用spring-boot-devtools进行热部署
- unity 人物IK设置 与乒乓函数