1 . 基于Android7.0 有系统签名才能静默安装

直接上代码

下载文件

 /**     *下载文件     *这里我是用的子线程     **/    public  void DownApkorSysrom( final  String  loadApkUrl ,final String savePath,final String saveFileName){        Thread thread=new Thread(new Runnable() {            @Override            public void run() {                try {                    URL url = new URL(loadApkUrl);                    Log.i(TAG,"updateManager download file url:"+loadApkUrl);                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();                    conn.connect();                    int length = conn.getContentLength();                    InputStream is = conn.getInputStream();                    File file;                    String filename;                    File downFile;                        file = new File(savePath);                        if (!file.exists()) {                            file.mkdir();                        }                        filename = saveFileName;                        downFile = new File(file,filename);                                      FileOutputStream fos = new FileOutputStream(downFile);                    int count = 0;                    byte buf[] = new byte[1024];                    do {                        int numread = is.read(buf);                        count += numread;                        // 更新进度                        int  progress = (int) (((float) count / length) * 100);                        Log.i(TAG,"down load progress:"+progress+" numread:"+numread);                        if (numread <= 0) {                           // 下载完成通知安装                            apkInstall(savePath+saveFileName);                            break;                        }                        fos.write(buf, 0, numread);                    } while (true);// 点击取消就停止下载.                } catch (MalformedURLException e) {                   Log.i(TAG,"down load MalformedURLException:"+e.toString());                    e.printStackTrace();                } catch (IOException e) {                   Log.i(TAG,"down load IOException TTTT:"+e.toString());                    e.printStackTrace();                }            }        });        thread.start();    }

2 . 静默安装

 /**     * 执行命令并且输出结果     */    public  boolean apkInstall(String path) {        Log.d(TAG,"SilentInstallActivity path:"+path);        Process p=null;        int value=-1;        try {            String docmd = "pm install -r -i com.amtcloud.amtphoneagent  --user 0 " +path;              p = Runtime.getRuntime().exec(docmd);             value = p.waitFor();        }catch (Exception e) {            Log.d(TAG, "SilentInstallActivity error: " + e);        }finally {            if(p!=null){                p.destroy();            }        }        return returnResult(value);    }

3 监听安装完成广播

public class SilenceInstallReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        // 接收安装广播        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")||                intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {//            startApp("com.amtzhmt.launcher",context);            Toast.makeText(context, "安装完成", Toast.LENGTH_LONG).show();        }    }    public void startApp(String appPackageName,Context context){        try{            Intent intent = context.getPackageManager().getLaunchIntentForPackage(appPackageName);            context.startActivity(intent);        }catch(Exception e){            Toast.makeText(context, "安装完成自启失败", Toast.LENGTH_LONG).show();        }    }}

 

更多相关文章

  1. Android文件读写权限
  2. android 文件选择
  3. Android UI 线程更新UI也会崩溃???
  4. Android 实现文件的下载
  5. 《android的SQLite与文件下载》
  6. Android Studio 与git 添加忽略文件
  7. android 入门学习笔记 上传大文件
  8. android实现文件读写功能
  9. Android 本地文件上传到服务器

随机推荐

  1. 浅谈android的selector,背景选择器
  2. android仿今日头条App、多种漂亮加载效果
  3. Android样式基础--shape篇
  4. Android发布, Android中国网站正式成立,
  5. 腾讯面试官:Binder的系统服务是如何获取的
  6. Android(安卓)应用程序基础
  7. Android(安卓)X86
  8. android:layout_gravity和android:gravit
  9. Android应用程序开发实用案例50则
  10. (mac)Android(安卓)Studio安装以及Fetchi