github https://github.com/AriaLyy/Aria
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 清单文件中加入
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="这里是你的包名.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
2 创建文件夹
3 创建 file_paths
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="hm_DCIM" path="DCIM/camerademo" />
<external-path name="hm_DCIM" path="apk/camerademo" />
<external-path name="hm_Pictures" path="Pictures/camerademo" />
<external-path name="hm_Pictures" path="Pictures/camerademo" />
<files-path name="hm_private_files" path="images" />
<files-path name="hm_private_files" path="apk.apk" />
<cache-path name="hm_private_cache" path="images" />
<external-files-path name="hm_external_files" path="Pictures" />
</paths>
作者:慕流蓝
来源:CSDN
原文:https://blog.csdn.net/qq953655369/article/details/81165675
版权声明:本文为博主原创文章,转载请附上博文链接!