下载安装APK

protected void downloadApk() {
        //apk下载链接地址,放置apk的所在路径
        
        //1,判断sd卡是否可用,是否挂在上
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            //2,获取sd路径
            String path = Environment.getExternalStorageDirectory().getAbsolutePath()
                    +File.separator+"mobilesafe74.apk";
            //3,发送请求,获取apk,并且放置到指定路径
            HttpUtils httpUtils = new HttpUtils();
            //4,发送请求,传递参数(下载地址,下载应用放置位置)
            httpUtils.download(mDownloadUrl, path, new RequestCallBack<File>() {
                @Override
                public void onSuccess(ResponseInfo<File> responseInfo) {
                    //下载成功(下载过后的放置在sd卡中apk)
                    Log.i(tag, "下载成功");
                    File file = responseInfo.result;
                    //提示用户安装
                    installApk(file);
                }
                @Override
                public void onFailure(HttpException arg0, String arg1) {
                    Log.i(tag, "下载失败");
                    //下载失败
                }
                //刚刚开始下载方法
                @Override
                public void onStart() {
                    Log.i(tag, "刚刚开始下载");
                    super.onStart();
                }
                
                //下载过程中的方法(下载apk总大小,当前的下载位置,是否正在下载)
                @Override
                public void onLoading(long total, long current,boolean isUploading) {
                    Log.i(tag, "下载中........");
                    Log.i(tag, "total = "+total);
                    Log.i(tag, "current = "+current);
                    super.onLoading(total, current, isUploading);
                }
            });
            
        }
    }

    /**
     * 安装对应apk
     * @param file    安装文件
     */
    protected void installApk(File file) {
        //系统应用界面,源码,安装apk入口
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        /*//文件作为数据源
        intent.setData(Uri.fromFile(file));
        //设置安装的类型
        intent.setType("application/vnd.android.package-archive");*/
        intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
//        startActivity(intent);
        startActivityForResult(intent, 0);
    }

 

//开启一个activity后,返回结果调用的方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        enterHome();
        super.onActivityResult(requestCode, resultCode, data);
    }

 

转载于:https://www.cnblogs.com/xufengyuan/p/6215787.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android应用开发中,下载并安装APK以升级应用版本通常涉及几个步骤:下载APK文件、安装APK文件以及确保新版本与Android 30的兼容性。以下是一个简化的过程和示例代码来说明如何实现这样的升级机制。 1. **下载APK文件**:首先,你需要从服务器下载新版本的APK文件。这通常通过网络请求来实现,比如使用OkHttp库下载文件。 ```java // 示例代码,用于从网络下载APK文件 OkHttpClient client = new OkHttpClient(); String url = "http://yourserver.com/yourapp.apk"; // 替换为实际的APK下载链接 Request request = new Request.Builder() .url(url) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 处理下载失败的情况 } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { // 下载成功,保存APK文件到应用的私有目录 File apkFile = new File(getFilesDir(), "update.apk"); try (FileOutputStream fos = new FileOutputStream(apkFile); InputStream is = response.body().byteStream()) { byte[] buffer = new byte[1024]; int len; while ((len = is.read(buffer)) != -1) { fos.write(buffer, 0, len); } } // 下载完成后,调用安装APK的方法 installApk(apkFile); } } }); ``` 2. **安装APK文件**:一旦APK文件下载并保存在设备上,你可以使用`Intent`和`Uri`来启动安装过程。注意,从Android N(7.0)开始,需要适配“FileProvider”来分享文件。 ```java // 示例代码,用于安装APK文件 private void installApk(File apkFile) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri apkUri = FileProvider.getUriForFile(this, getPackageName() + ".provider", apkFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); startActivity(intent); } else { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } } ``` 3. **适配Android 30**:为了确保新版本应用与Android 30的兼容性,你需要遵循新的权限模型、存储访问框架(Scoped Storage)以及隐私相关的更新。例如,更新你的应用以使用分区存储。 ```xml <!-- 在AndroidManifest.xml中添加属性以启用分区存储 --> <manifest ...> <application android:requestLegacyExternalStorage="true"> ... </application> </manifest> ``` 请注意,`requestLegacyExternalStorage`是一个过渡属性,用于在Android 10上请求完整的外部存储访问权限。对于Android 11及以上版本,你可能需要使用分区存储或者动态请求权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值