From b5b6f0f88f28fff3172818f2ee010d6c133ee797 Mon Sep 17 00:00:00 2001 From: zqzess Date: Thu, 3 Jun 2021 02:57:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=90=8E=E8=87=AA=E5=8A=A8=E5=AE=89=E8=A3=85=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=8C=85=E5=87=BA=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 +-- app/release/output.json | 2 +- app/src/main/AndroidManifest.xml | 15 +++++++++ .../main/java/com/tdkankan/MainActivity.java | 8 ++--- .../com/tdkankan/Service/DownloadService.java | 7 ++++ .../java/com/tdkankan/UI/SettingFragment.java | 33 +++++++++++++++---- app/src/main/res/xml/file_paths.xml | 8 +++++ 7 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 app/src/main/res/xml/file_paths.xml diff --git a/app/build.gradle b/app/build.gradle index f24753d..353a8cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.tdkankan" minSdkVersion 21 targetSdkVersion 29 - versionCode 3 - versionName "1.0.2" + versionCode 4 + versionName "1.0.3" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/release/output.json b/app/release/output.json index e5c84a8..557dcb3 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":3,"versionName":"1.0.2","enabled":true,"outputFile":"TDkankan_1.0.2.apk","fullName":"release","baseName":"release"},"path":"TDkankan_1.0.2.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":4,"versionName":"1.0.3","enabled":true,"outputFile":"TDkankan_1.0.3.apk","fullName":"release","baseName":"release"},"path":"TDkankan_1.0.3.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 91c813e..487d6e7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,12 @@ + + + + + + @@ -44,6 +50,15 @@ android:enabled="true" > + + + \ No newline at end of file diff --git a/app/src/main/java/com/tdkankan/MainActivity.java b/app/src/main/java/com/tdkankan/MainActivity.java index 1bbca17..53555d7 100644 --- a/app/src/main/java/com/tdkankan/MainActivity.java +++ b/app/src/main/java/com/tdkankan/MainActivity.java @@ -45,10 +45,10 @@ protected void onCreate(Bundle savedInstanceState) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); initTabItemData(); GlobalConfig.bitmapnull=getBitmapFromRes(this,R.drawable.nonepic); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); - StrictMode.setVmPolicy( builder.build() ); - } +// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { +// StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); +// StrictMode.setVmPolicy( builder.build() ); +// } } private void initTabItemData() { diff --git a/app/src/main/java/com/tdkankan/Service/DownloadService.java b/app/src/main/java/com/tdkankan/Service/DownloadService.java index 6cdc226..87dad6a 100644 --- a/app/src/main/java/com/tdkankan/Service/DownloadService.java +++ b/app/src/main/java/com/tdkankan/Service/DownloadService.java @@ -12,6 +12,8 @@ import androidx.annotation.Nullable; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import java.io.File; + /** * @author ZQZESS * @date 2021/6/3. @@ -42,6 +44,11 @@ protected void onHandleIntent(@Nullable Intent intent) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); //指定APK缓存路径和应用名称,可在SD卡/Android/data/包名/file/Download文件夹中查看 + File file = new File(this.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "TDkankan.apk"); + if(file.exists()&& file.isFile()) + { + file.delete(); + } request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, "TDkankan.apk"); // 设置允许使用的网络类型,这里是移动网络和wifi都可以 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE diff --git a/app/src/main/java/com/tdkankan/UI/SettingFragment.java b/app/src/main/java/com/tdkankan/UI/SettingFragment.java index 9b28216..c48a57c 100644 --- a/app/src/main/java/com/tdkankan/UI/SettingFragment.java +++ b/app/src/main/java/com/tdkankan/UI/SettingFragment.java @@ -3,6 +3,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; +import androidx.core.content.FileProvider; import androidx.fragment.app.Fragment; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -13,6 +14,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.util.Log; @@ -160,14 +162,33 @@ private class MyReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String data = intent.getStringExtra(DownloadService.EXTENDED_DATA_STATUS); // Log.i("test", data); - long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); // Toast.makeText(context, "编号:"+id+"的下载任务已经完成!", Toast.LENGTH_SHORT).show(); - intent = new Intent(Intent.ACTION_VIEW); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/TDkankan.apk")), - "application/vnd.android.package-archive"); - startActivity(intent); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "TDkankan.apk"); +// Uri apkUri = FileProvider.getUriForFile(context, +// "com.tdkankan.FileProvider", new File(Environment.getExternalStorageDirectory() + "/TDkankan.apk")); + Uri apkUri = FileProvider.getUriForFile(context, "com.tdkankan.FileProvider", file); + Intent install = new Intent(Intent.ACTION_VIEW); + install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + //添加这一句表示对目标应用临时授权该Uri所代表的文件 + install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + install.setDataAndType(apkUri, "application/vnd.android.package-archive"); + context.startActivity(install); + + } else { + Intent install = new Intent(Intent.ACTION_VIEW); +// install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/TDkankan.apk")), "application/vnd.android.package-archive"); + intent.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS, "TDkankan.apk")), "application/vnd.android.package-archive"); + install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(install); + } +// intent = new Intent(Intent.ACTION_VIEW); +// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); +// intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/TDkankan.apk")), +// "application/vnd.android.package-archive"); +// startActivity(intent); } } diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml new file mode 100644 index 0000000..6f51582 --- /dev/null +++ b/app/src/main/res/xml/file_paths.xml @@ -0,0 +1,8 @@ + + + +