哥們點個贊再走呀
制作不容易宪卿,條件允許情況下,打賞一下万栅,謝謝
作者使用xUtils下載文件佑钾,如使用其他框架下載,可在下載邏輯進行修改
- AndroidManifest
<!--使用網(wǎng)絡(luò)-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 往SDCard 讀寫入數(shù)據(jù)權(quán)限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--安裝apk權(quán)限-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<application
android:usesCleartextTraffic="true"
...>
<!--安裝apk-->
<!--非Androidx使用這個 android:name="android.support.v4.content.FileProvider" -->
<!-- Androidx使用這個 android:name="androidx.core.content.FileProvider" -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
- file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="." name="external_storage_root" />
</paths>
- 下載邏輯和安裝
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
permission();
}
//點擊下載按鈕
public void onDownload(View view) {
if (permission() == false) {
Toast.makeText(this, "未獲取權(quán)限", Toast.LENGTH_SHORT).show();
return;
}
downLoad();
}
//下載文件
private void downLoad() {
//apk下載鏈接
String apkUrl = "http://xxx.apk";
//文件路徑
String filePath = getExternalCacheDir() + "/Download/file.apk";
//下載
RequestParams params = new RequestParams(apkUrl);
params.setSaveFilePath(filePath);//保存路徑
x.http().post(params, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
installAPK(MainActivity.this, result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
Log.i("MainActivity", "正在下載:" + (int)((float)current/total * 100));
}
});
}
//安裝apk
public void installAPK(Context context, File apkFile) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {// 判斷版本大于等于7.0
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileProvider", apkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK);
} else {
uri = Uri.parse("file://" + apkFile);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
intent.setDataAndType(uri, "application/vnd.android.package-archive");
context.startActivity(intent);
}
//獲取權(quán)限
private boolean permission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int REQUEST_CODE_CONTACT = 101;//授權(quán)回調(diào)標識
//需要的權(quán)限
String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE //下載的apk保存到手機的權(quán)限
};
if (checkSelfPermission(permissions[0]) != PackageManager.PERMISSION_GRANTED) { //未授權(quán)
requestPermissions(permissions, REQUEST_CODE_CONTACT); //申請權(quán)限
return false;
} else {
return true;
}
}
return true;
}
}
- 制作不容易烦粒,條件允許情況下次绘,打賞一下,謝謝