Skip to content

Commit

Permalink
修复获取更新后自动安装解析包出错
Browse files Browse the repository at this point in the history
  • Loading branch information
zqzess committed Jun 2, 2021
1 parent 80727e5 commit b5b6f0f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -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":{}}]
[{"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":{}}]
15 changes: 15 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

<uses-permission android:name="android.permission.INTERNET" /> <!-- 往sdcard中写入数据的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 在sdcard中创建/删除文件的权限 -->
<!-- 8.0手机安装软件 -->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!-- 允许装载和卸载文件系统权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- Android8.0 未知来源权限-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission
android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
tools:ignore="ProtectedPermissions" />
Expand Down Expand Up @@ -44,6 +50,15 @@
android:enabled="true"
>
</service>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.tdkankan.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>

</manifest>
8 changes: 4 additions & 4 deletions app/src/main/java/com/tdkankan/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/tdkankan/Service/DownloadService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.io.File;

/**
* @author ZQZESS
* @date 2021/6/3.
Expand Down Expand Up @@ -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
Expand Down
33 changes: 27 additions & 6 deletions app/src/main/java/com/tdkankan/UI/SettingFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);

}
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<paths>
<external-path
name="files_root"
path="Android/data/com.tdkankan" />
<external-path
name="external_storage_root"
path="." />
</paths>

0 comments on commit b5b6f0f

Please sign in to comment.