安卓更新下载apk 并安装

github https://github.com/AriaLyy/Aria

或者  github https://github.com/maning0303/MNUpdateAPK

compile 'com.arialyy.aria:aria-core:3.4.8'
annotationProcessor 'com.arialyy.aria:aria-compiler:3.4.8'
public class MainActivity extends AppCompatActivity {
    String s="http://app.mi.com/download/634713";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String  STORE_PATH= Environment.getExternalStorageDirectory()+"/apk/camerademo"+"/a.apk";
        Aria.download(this)
                .load(s)     //读取下载地址
                .setFilePath(STORE_PATH) //设置文件保存的完整路径
                .start();   //启动下载
        Aria.download(this).register();

    }
    //在这里处理任务执行中的状态,如进度进度条的刷新
    @Download.onTaskRunning
    protected void running(DownloadTask task) {
        int p = task.getPercent(); //任务进度百分比
        Log.e("ddddddddddddddddd",String.valueOf(p));
    }

    @Download.onTaskComplete
    void taskComplete(DownloadTask task) {
        //在这里处理任务完成的状态
        Log.e("ddddddddddddddddd","下载完成");
        String downloadPath = task.getDownloadPath();
        Intent i = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= 24) { //适配安卓7.0
            i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_ACTIVITY_NEW_TASK);

            Uri apkFileUri = FileProvider.getUriForFile(getApplicationContext(),
                    getPackageName()+".fileprovider", new File( downloadPath));
            i.setDataAndType(apkFileUri, "application/vnd.android.package-archive");
        } else {
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            i.setAction(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("file://" + downloadPath.toString()),
                    "application/vnd.android.package-archive");// File.toString()会返回路径信息
        }
        startActivity(i);
    }
}

 

适配7.0 配置FileProvider

1 清单文件中加入


    

2 创建文件夹

安卓更新下载apk 并安装_第1张图片

3 创建 file_paths



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

 

你可能感兴趣的:(安卓更新下载apk 并安装)