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

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值