fix: chrome tab open failed due to chrome process not running

This commit is contained in:
Yumeng Huang 2023-09-03 19:12:03 +08:00
parent b00b3781ef
commit 4c712b6f36
2 changed files with 24 additions and 6 deletions

View File

@ -165,8 +165,16 @@ public class ChromeCustomTabsActivity extends Activity implements Disposable {
@Nullable Map<String, String> headers,
@Nullable String referrer,
@Nullable List<String> otherLikelyURLs) {
launchUrlWithSession(customTabsSession, url, headers, referrer, otherLikelyURLs);
}
public void launchUrlWithSession(@Nullable CustomTabsSession session,
@NonNull String url,
@Nullable Map<String, String> headers,
@Nullable String referrer,
@Nullable List<String> otherLikelyURLs) {
mayLaunchUrl(url, otherLikelyURLs);
builder = new CustomTabsIntent.Builder(customTabsSession);
builder = new CustomTabsIntent.Builder(session);
prepareCustomTabs();
CustomTabsIntent customTabsIntent = builder.build();
@ -328,7 +336,17 @@ public class ChromeCustomTabsActivity extends Activity implements Disposable {
@Override
protected void onStart() {
super.onStart();
customTabActivityHelper.bindCustomTabsService(this);
boolean isBindSuccess = customTabActivityHelper.bindCustomTabsService(this);
if (!isBindSuccess) {
// chrome process not running, start tab directly
if (initialUrl != null) {
launchUrlWithSession(null, initialUrl, initialHeaders, initialReferrer, initialOtherLikelyURLs);
//avoid webpage reopen: onServiceConnected->launchUrl
initialUrl = null;
}
}
}
@Override

View File

@ -122,14 +122,14 @@ public class CustomTabActivityHelper implements ServiceConnectionCallback {
* Binds the Activity to the Custom Tabs Service.
* @param activity the activity to be binded to the service.
*/
public void bindCustomTabsService(Activity activity) {
if (mClient != null) return;
public boolean bindCustomTabsService(Activity activity) {
if (mClient != null) return true;
String packageName = CustomTabsHelper.getPackageNameToUse(activity);
if (packageName == null) return;
if (packageName == null) return false;
mConnection = new ServiceConnection(this);
CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
return CustomTabsClient.bindCustomTabsService(activity, packageName, mConnection);
}
/**