Skip to content

Commit 3ec2793

Browse files
authored
Merge pull request #66 from Jiiiiiin/feature/handler_download_service_on_resp_can_not_use_start_activity_for_result
解决使用默认的UpdateDialogFragment在初次安装应用时候,因为DownloadService没有能使用activity.s…
2 parents 512e65c + 2d84210 commit 3ec2793

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

update-app/src/main/java/com/vector/update_app/SilenceUpdateCallback.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public boolean onFinish(File file) {
5353
public void onError(String msg) {
5454

5555
}
56+
57+
@Override
58+
public boolean onInstallAppAndAppOnForeground(File file) {
59+
return false;
60+
}
5661
});
5762
}
5863
}

update-app/src/main/java/com/vector/update_app/UpdateDialogFragment.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ public boolean onFinish(final File file) {
387387
@Override
388388
public void onClick(View v) {
389389
AppUpdateUtils.installApp(UpdateDialogFragment.this, file);
390+
dismiss();
390391
}
391392
});
392393
} else {
@@ -402,6 +403,14 @@ public void onError(String msg) {
402403
dismissAllowingStateLoss();
403404
}
404405
}
406+
407+
@Override
408+
public boolean onInstallAppAndAppOnForeground(File file) {
409+
// 如果应用处于前台,那么就自行处理应用安装
410+
AppUpdateUtils.installApp(UpdateDialogFragment.this.getActivity(), file);
411+
dismiss();
412+
return true;
413+
}
405414
});
406415
}
407416
}

update-app/src/main/java/com/vector/update_app/service/DownloadService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ public interface DownloadCallback {
189189
* @param msg 异常信息
190190
*/
191191
void onError(String msg);
192+
193+
/**
194+
* 当应用处于前台,准备执行安装程序时候的回调,
195+
*
196+
* @param file
197+
* @return
198+
*/
199+
boolean onInstallAppAndAppOnForeground(File file);
192200
}
193201

194202
/**
@@ -283,7 +291,10 @@ public void onResponse(File file) {
283291
if (AppUpdateUtils.isAppOnForeground(DownloadService.this) || mBuilder == null) {
284292
//App前台运行
285293
mNotificationManager.cancel(NOTIFY_ID);
286-
AppUpdateUtils.installApp(DownloadService.this, file);
294+
boolean temp = mCallBack.onInstallAppAndAppOnForeground(file);
295+
if (!temp) {
296+
AppUpdateUtils.installApp(DownloadService.this, file);
297+
}
287298
} else {
288299
//App后台运行
289300
//更新参数,注意flags要使用FLAG_UPDATE_CURRENT

update-app/src/main/java/com/vector/update_app/utils/AppUpdateUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ public static boolean installApp(Context context, File appFile) {
9696
return false;
9797
}
9898

99+
public static boolean installApp(Activity activity, File appFile) {
100+
try {
101+
Intent intent = new Intent(Intent.ACTION_VIEW);
102+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
103+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
104+
Uri fileUri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".fileProvider", appFile);
105+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
106+
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
107+
} else {
108+
intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive");
109+
}
110+
if (activity.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
111+
activity.startActivityForResult(intent, REQ_CODE_INSTALL_APP);
112+
}
113+
return true;
114+
} catch (Exception e) {
115+
// TODO 后续可以考虑这种情况应该通知应用开发者
116+
e.printStackTrace();
117+
}
118+
return false;
119+
}
120+
99121
public static boolean installApp(Fragment fragment, File appFile) {
100122
try {
101123
final Activity activity = fragment.getActivity();

0 commit comments

Comments
 (0)