Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决使用默认的UpdateDialogFragment在初次安装应用时候,因为DownloadService没有能使用activity.s… #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public boolean onFinish(File file) {
public void onError(String msg) {

}

@Override
public boolean onInstallAppAndAppOnForeground(File file) {
return false;
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ public boolean onFinish(final File file) {
@Override
public void onClick(View v) {
AppUpdateUtils.installApp(UpdateDialogFragment.this, file);
dismiss();
}
});
} else {
Expand All @@ -402,6 +403,14 @@ public void onError(String msg) {
dismissAllowingStateLoss();
}
}

@Override
public boolean onInstallAppAndAppOnForeground(File file) {
// 如果应用处于前台,那么就自行处理应用安装
AppUpdateUtils.installApp(UpdateDialogFragment.this.getActivity(), file);
dismiss();
return true;
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ public interface DownloadCallback {
* @param msg 异常信息
*/
void onError(String msg);

/**
* 当应用处于前台,准备执行安装程序时候的回调,
*
* @param file
* @return
*/
boolean onInstallAppAndAppOnForeground(File file);
}

/**
Expand Down Expand Up @@ -283,7 +291,10 @@ public void onResponse(File file) {
if (AppUpdateUtils.isAppOnForeground(DownloadService.this) || mBuilder == null) {
//App前台运行
mNotificationManager.cancel(NOTIFY_ID);
AppUpdateUtils.installApp(DownloadService.this, file);
boolean temp = mCallBack.onInstallAppAndAppOnForeground(file);
if (!temp) {
AppUpdateUtils.installApp(DownloadService.this, file);
}
} else {
//App后台运行
//更新参数,注意flags要使用FLAG_UPDATE_CURRENT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,28 @@ public static boolean installApp(Context context, File appFile) {
return false;
}

public static boolean installApp(Activity activity, File appFile) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri fileUri = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".fileProvider", appFile);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.fromFile(appFile), "application/vnd.android.package-archive");
}
if (activity.getPackageManager().queryIntentActivities(intent, 0).size() > 0) {
activity.startActivityForResult(intent, REQ_CODE_INSTALL_APP);
}
return true;
} catch (Exception e) {
// TODO 后续可以考虑这种情况应该通知应用开发者
e.printStackTrace();
}
return false;
}

public static boolean installApp(Fragment fragment, File appFile) {
try {
final Activity activity = fragment.getActivity();
Expand Down