我目前正在为 Android 开发一个简单的 RSS 应用程序,该应用程序的一个功能是使用 chrome 自定义选项卡打开 url;我已经根据文档上提供的示例实现了 Chrome 自定义选项卡。
虽然大多数 url 都通过将其解析为 Uri 成功显示,但当自定义选项卡尝试打开时,我传递的其中一个 url 导致崩溃,看起来像这种格式:
标签:github.com,2008:PullRequestReviewCommentEvent/4172209621
我猜我不应该只用Uri.parse()
方法解析这个字符串 url,但我有点卡住了,想知道在这里做什么。
我也猜这是一个与此类似的问题: Chrome custom tabs not opening other apps
崩溃似乎没有可以处理此意图的可用活动:
FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=tag:github.com,2008:PullRequestReviewCommentEvent/4172209621 (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1798)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1512)
at android.app.Activity.startActivityForResult(Activity.java:3930)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:48)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
at android.app.Activity.startActivityForResult(Activity.java:3890)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
at android.app.Activity.startActivity(Activity.java:4213)
at android.support.v4.app.ActivityCompatJB.startActivity(ActivityCompatJB.java:27)
at android.support.v4.app.ActivityCompat.startActivity(ActivityCompat.java:134)
at android.support.customtabs.CustomTabsIntent.launchUrl(CustomTabsIntent.java:244)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21155)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
以下是我迄今为止编写的关于使用 Chrome 自定义选项卡打开 URL 的源代码:
public class ArticleFragment extends Fragment {
@OnClick(R.id.button_see_more) public void onClickSeeMore() {
launchCustomTabs(article.url());
}
private CustomTabsServiceConnection customTabsConnection;
private CustomTabsClient customTabsClient;
private CustomTabsSession customTabsSession;
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
...
bindCustomTabsService();
}
@Override public void onDestroy() {
unbindCustomTabsService();
super.onDestroy();
}
private void bindCustomTabsService() {
if (customTabsClient != null) {
return;
}
customTabsConnection = new CustomTabsServiceConnection() {
@Override public void onCustomTabsServiceConnected(ComponentName componentName,
CustomTabsClient customTabsClient) {
ArticleSummaryDetailFragment.this.customTabsClient = customTabsClient;
customTabsSession = customTabsClient.newSession(new CustomTabsCallback() {
@Override public void onNavigationEvent(int navigationEvent, Bundle extras) {
Timber.wtf("onNavigationEvent: Code = " + navigationEvent);
}
});
customTabsSession.mayLaunchUrl(Uri.parse(article.originId()), null, null);
customTabsClient.warmup(0L);
}
@Override public void onServiceDisconnected(ComponentName componentName) {
customTabsClient = null;
}
};
CustomTabsClient.bindCustomTabsService(getActivity(), getActivity().getPackageName(),
customTabsConnection);
}
private void unbindCustomTabsService() {
if (customTabsConnection == null) {
return;
}
getActivity().unbindService(customTabsConnection);
customTabsClient = null;
customTabsSession = null;
}
private void launchCustomTabs(String url) {
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder(customTabsSession).build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
}
}