Android下载 文件(APP) 并且静默安装

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();
        }
    }
}

 

你可能感兴趣的:(Android)