记录一下android自动更新功能的实现,以及出现问题

网上天花乱坠,东边抄西边的,西边抄东边的,到处抄,代码还乱。

这里终于解决了,我把固定的代码贴在下面,并说明几个部分的问题

第一步、检查一下,签名,这一步我不太确定不加会不会有问题,加了一定没问题,读者可以先跳过这一步直接进入下一步,直到最后如果还有问题,再回来尝试一下这一步。
在这里插入图片描述
点击之后,会跳转到如下页面
在这里插入图片描述
选择apk,点击next,出现如下页面,
在这里插入图片描述
如果是空的,就点击Create new,随便填写一下内容,也会产生一个签名,
然后点击next
在这里插入图片描述
把V1和V2都勾选起来。然后finish,这一步就结束了。

第二步,在manifest.xml添加语句
在这里插入图片描述

然后在res中创建一个同名的xml文件
在这里插入图片描述
修改成XML
这个文件的内容如下,大家直接复制就好,

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
</paths>

第三步
apk下载,我是采取用自己写socket下载,代码很固定
这里要注意的问题如下:
1、
要用这个写法,如果写的是getFileDir()或者写的是getCacheDir()后面跳转到安装界面就会报“解析软件有问题”。

在这里插入图片描述
2、防止下载下来的apk包的权限限制,要在安装之前,加上这么一条语句

在这里插入图片描述
下面那段if直接照抄就好了,下面几个地方修改成你的包名和File就好了
在这里插入图片描述

 class DownLoadApkThread extends Thread{
        public void run(){
            apkFile = new File(Environment.getExternalStorageDirectory(),"update.apk");
            Log.d("看看apk下载路径:",apkFile+"");
            Socket socket = new Socket();
            InetSocketAddress isa = new InetSocketAddress(ForInet.ip,ForInet.port);

            try {

                socket.connect(isa, ForInet.delayMilli);
                String msg = ForInet.checkversionProtocol+"download"+ForInet.checkversionProtocol;
                PrintStream ps = new PrintStream(socket.getOutputStream());
                ps.println(msg);


                //从服务器上下载最新的apk安装包

                try(
                        BufferedInputStream bufferedInputStream = new BufferedInputStream(socket.getInputStream());
                        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(apkFile)))
                {
                    int len = 0;
                    int hasRead = 0;
                    byte[] buffer = new byte[1024];
                    while((hasRead = bufferedInputStream.read(buffer))!=-1){
                        outputStream.write(buffer,0,hasRead);
                        len+=hasRead;
                        Log.d("下载进度:",len+"");
                    }
                    outputStream.flush();
                    Log.d("apk下载完成!","");
                }
                catch (IOException ioe){
                    ioe.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
			//设置文件的读写权限
            setPermission(apkFile.toString());
            //完成后,跳转到安装界面
            if(Build.VERSION.SDK_INT>=24) {//判读版本是否在7.0以上
                Uri apkUri = FileProvider.getUriForFile(MainActivity.this, "com.example.study4dome2.fileprovider", apkFile);
                Intent install = new Intent(Intent.ACTION_VIEW);

                install.setDataAndType(apkUri, "application/vnd.android.package-archive");
                install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//添加这一句表示对目标应用临时授权该Uri所代表的文件
                startActivity(install);
            }else {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse("file://"+apkFile.toString()), "application/vnd.android.package-archive");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

            }


        }
        private  void setPermission(String filePath)
        {
            String  command = "chmod " + "777" + " " + filePath;
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec(command);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

这里要注意一下write(buffer,0,hasRead); 我不知道为什么写成write(buffer) ; apk打都打不开。
接下来应该就不会报错了
共同努力,家人们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

细水长流cpu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值