android apk更新下载以及安装

权限

    <uses-permission android:name="android.permission.REPLACE_EXISTING_PACKAGE"/>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- 获取网络状态 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.READ_LOGS" />

下载 保持apk路径要对应 android:resource="@xml/provider_paths"
getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) 对应 /storage/emulated/0/Android/data/你的包名/files/目录

  OutputStream output = null;
                  InputStream input = null;
                  try{
                       URL    url = new URL(path);
                       HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
                       input = urlConn.getInputStream();
                      long size = urlConn.getContentLength();//文件大小
                      File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xx.apk");
                       output = new FileOutputStream(file);
                       byte [] buffer = new byte[1024];
                      long total = 0;
                      int count = 0;
                       while((count = input.read(buffer)) != -1){
                           output.write(buffer,0,count);
                           output.flush();
                              total += count;
                              int  pro =  (int)(total*100/size);
                           
                       }
                     } catch (IOException e) {
                          e.printStackTrace();

                     }   finally {
                          try {
                              output.close();
                              input.close();
                            
                          } catch (IOException e) {
                              e.printStackTrace();
                          }
                      }

安装 getUriForFile 填上AndroidManifest.xml android:authorities

 File file = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xxx.apk");
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    Uri uri = AppFileProvider.getUriForFile(BleActivity.this, "com.a.mybledemo.fileprovider", file);
                    intent.setDataAndType(uri, "application/vnd.android.package-archive");
                    startActivity(intent);

AndroidManifest.xml添加文件提供者FileProvider ,FileProvider是系统自带的

 <provider
            android:name="FileProvider"
            android:authorities="com.sbas.mybledemo.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"
                tools:replace="name,resource" />
        </provider>

res/xml/provider_paths.xml external-files-path对应对应 /storage/emulated/0/Android/data/你的包名/files/目录

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path
        name="download"
        path="/Download"
        />
</paths>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值