Chrome自定义标签在未安装Chrome时抛出错误:找不到处理Intent的活动

问题描述 投票:2回答:2

Chrome自定义选项卡在安装chrome时工作正常,但是当没有安装chrome时,它会引发错误

CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
intentBuilder.setShowTitle(true);
CustomTabActivityHelper.openCustomTab(activityy, intentBuilder.build(), Uri.parse(link), new WebviewFallback());

LogCat错误信息

FATAL EXCEPTION: main
Process: opensource.itspr.recycler, PID: 13114
   android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=http://www.google.com/... pkg=com.android.chrome (has extras) }
       at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1889)
       at android.app.Instrumentation.execStartActivity(Instrumentation.java:1579)
       at android.app.Activity.startActivityForResult(Activity.java:3921)
       at android.app.Activity.startActivityForResult(Activity.java:3881)
       at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:784)
       at android.app.Activity.startActivity(Activity.java:4208)
       at android.app.Activity.startActivity(Activity.java:4176)
       at android.support.customtabs.CustomTabsIntent.launchUrl(CustomTabsIntent.java:165)
       at opensource.itspr.recycler.Util.customtabs.CustomTabActivityHelper.openCustomTab(CustomTabActivityHelper.java:41)
       at opensource.itspr.recycler.HolderNews.ItemLink$1.onClick(ItemLink.java:55)
       at android.view.View.performClick(View.java:5201)
       at android.view.View$PerformClick.run(View.java:21163)
       at android.os.Handler.handleCallback(Handler.java:746)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5443)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)                                                     

错误信息图像

Web view fallback.Java

public class WebviewFallback implements CustomTabActivityHelper.CustomTabFallback {
@Override
public void openUri(Activity activity, Uri uri) {
    Log.d("I came here", String.valueOf(uri));
    Intent intent = new Intent(activity, WebViewActivity.class);
    intent.putExtra(WebViewActivity.EXTRA_URL, uri.toString());
    activity.startActivity(intent);
}

}

Web view activity.Java

public class WebViewActivity extends AppCompatActivity {
public static final String EXTRA_URL = "extra.url";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    String url = getIntent().getStringExtra(EXTRA_URL);
    WebView webView = (WebView)findViewById(R.id.web_view);
    webView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    setTitle(url);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    webView.loadUrl(url);
}
android chrome-custom-tabs
2个回答
5
投票

我假设您的问题是“我如何检测并处理Chrome未安装?”所以你去......

关键可能是PackageManager.queryIntentActivities (Intent intent, int flags)

检索可以为给定意图执行的所有活动。

参数

intent - 根据resolveActivity()所需的意图。

flags - 附加选项标志。最重要的是MATCH_DEFAULT_ONLY,将解决方案限制为仅支持CATEGORY_DEFAULT的那些活动。您还可以设置MATCH_ALL以防止过滤结果。

返回

一个List<ResolveInfo>包含每个匹配Activity的一个条目。这些是从最佳匹配到最差匹配的顺序 - 也就是说,列表中的第一项是resolveActivity(Intent, int)返回的内容。如果没有匹配的活动,则返回空列表。

像这样的东西:

CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
intentBuilder.setShowTitle(true);
final Intent customTabsIntent = intentBuilder.build();
final List<ResolveInfo> customTabsApps = activityy.getPackageManager().queryIntentActivities(customTabsIntent, 0);

if (customTabsApps.size() > 0) {
    CustomTabActivityHelper.openCustomTab(activityy, customTabsIntent, Uri.parse(link), new WebviewFallback());
} else {
    // Chrome not installed. Display a toast or something to notify the user
}

0
投票

您必须首先检查chrome是否已安装。此代码可以帮助您检查是否安装了应用程序。

isAppInstalled("com.android.chrome");
private boolean isAppInstalled(String packageName) {
PackageManager pm = getPackageManager();
boolean installed = false;
try {
   pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
   installed = true;
} catch (PackageManager.NameNotFoundException e) {
   installed = false;
}
return installed;
}
© www.soinside.com 2019 - 2024. All rights reserved.