美文网首页
下载及安装APK

下载及安装APK

作者: 大叔不秃 | 来源:发表于2019-04-26 17:24 被阅读0次

处于工作需求在程序里面下载安装程序,貌似这种需求还是第一次接触 废话也不多说了直接上代码吧,也没多少东西,挺简单在此记录一下

效果图

微信图片.png

一、需求

1、在APP里面下载APK以及安装APK(不做过多解释直接上代码)

public class MainActivity extends Activity {
//  按钮
    private Button button;
//  上下文
    private Context mContext;
//  进度条
    private ProgressBar mProgressBar;
//  对话框
    private Dialog mDownloadDialog;
//  判断是否停止
    private boolean mIsCancel = false;
//  进度
    private int mProgress;
//  文件保存路径
    private String mSavePath;
//  版本名称
    private String mVersion_name="1.0";
//  请求链接
   private String url ="https://download.dgstaticresources.net/fusion/android/app-c6-release.apk";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mIsCancel=false;
//              展示对话框
                showDownloadDialog();
            }
        });
    }


    /*
     * 显示正在下载对话框
     */
    protected void showDownloadDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("下载中");
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_progress, null);
        mProgressBar = (ProgressBar) view.findViewById(R.id.id_progress);
        builder.setView(view);

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 隐藏当前对话框
                dialog.dismiss();
                // 设置下载状态为取消
                mIsCancel = true;
            }
        });

        mDownloadDialog = builder.create();
        mDownloadDialog.show();

        // 下载文件
        downloadAPK();
    }
    /*
     * 开启新线程下载apk文件
     */
    private void downloadAPK() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                        String sdPath = Environment.getExternalStorageDirectory() + "/";
//                      文件保存路径
                        mSavePath = sdPath + "jikedownload";

                        File dir = new File(mSavePath);
                        if (!dir.exists()){
                            dir.mkdir();
                        }
                        // 下载文件
                        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
                        conn.connect();
                        InputStream is = conn.getInputStream();
                        int length = conn.getContentLength();

                        File apkFile = new File(mSavePath, mVersion_name);
                        FileOutputStream fos = new FileOutputStream(apkFile);

                        int count = 0;
                        byte[] buffer = new byte[1024];
                        while (!mIsCancel){
                            int numread = is.read(buffer);
                            count += numread;
                            // 计算进度条的当前位置
                            mProgress = (int) (((float)count/length) * 100);
                            // 更新进度条
                            mUpdateProgressHandler.sendEmptyMessage(1);

                            // 下载完成
                            if (numread < 0){
                                mUpdateProgressHandler.sendEmptyMessage(2);
                                break;
                            }
                            fos.write(buffer, 0, numread);
                        }
                        fos.close();
                        is.close();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 接收消息
     */
    private Handler mUpdateProgressHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    // 设置进度条
                    mProgressBar.setProgress(mProgress);
                    break;
                case 2:
                    // 隐藏当前下载对话框
                    mDownloadDialog.dismiss();
                    // 安装 APK 文件
                    installAPK();
            }
        };
    };


    /*
     * 下载到本地后执行安装
     */
    protected void installAPK() {
        File apkFile = new File(mSavePath, mVersion_name);
        if (!apkFile.exists()){
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
//      安装完成后,启动app(源码中少了这句话)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.parse("file://" + apkFile.toString());
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        mContext.startActivity(intent);
    }


}//calss

总结:其实也没什么难点 在此记录一下 方便自己以后阅读

相关文章

  • 下载及安装APK

    处于工作需求在程序里面下载安装程序,貌似这种需求还是第一次接触 废话也不多说了直接上代码吧,也没多少东西,挺简单在...

  • Android版本更新

    添加依赖: 在project—build.gradle下配置 添加下载apk权限及注册内容提供者(安装APK在An...

  • apk安装

    apk安装场景:app升级时,下载apk后,提示用户安装。 通过代码实现apk安装,注意,必须判断手机androi...

  • Android中下载apk文件并安装

    Android中下载apk文件并安装

  • Android7.0下载Apk自动安装

    Android7.0下载Apk自动安装 1. 整体需求 下载APK文件使用DownloadManager来下载在应...

  • APK 瘦身

    为什么要减小 APK 体积 下载 APK 需要流量,流量需要花钱; 安装 APK 需要考虑手机剩余空间 APK 大...

  • Android7.0下载Apk自动安装

    Android7.0下载Apk自动安装

  • VirtualXposed强制调试WebView

    真机安装VirtualXposed VX上安装WebViewDebugHook.apk,并启用image下载地址:...

  • app 测试内容

    1.安装/卸载/更新 1.安装测试: 2. 装包的来源:应用商店下载/下载.apk文件...

  • drozer 使用小结

    drozer 下载地址 1、安装 pc端安装drozer Android设备中安装agent.apk adbins...

网友评论

      本文标题:下载及安装APK

      本文链接:https://www.haomeiwen.com/subject/udyqnqtx.html