sho*_*ano 6 android chrome-custom-tabs
我目前正在为Android开发一个简单的RSS应用,该应用的功能之一是使用chrome自定义标签打开网址;我已经根据文档中提供的示例实现了Chrome自定义标签。
虽然大多数url通过将其解析为Uri都已成功显示,但是我传递的其中一个url在自定义标签试图打开时导致崩溃,该格式类似于以下格式:
标记:github.com,2008:PullRequestReviewCommentEvent / 4172209621
我猜我不应该只用Uri.parse()方法解析这个String url ,而是有点被困住了,想知道在这里做什么。
我也猜想这是与此类似的问题: Chrome自定义标签无法打开其他应用
崩溃似乎没有可以解决此意图的可用活动:
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)
Run Code Online (Sandbox Code Playgroud)
以下是我到目前为止编写的有关使用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));
}
}
Run Code Online (Sandbox Code Playgroud)
这是android的一般行为。如果您启动的URI无法由任何单个应用程序解析,则您需要妥善处理该错误。在这种情况下,您无能为力,这取决于用户是否拥有可以实际解释该URI的应用程序。
您需要处理该异常。这不是图书馆的问题。另外,作为chrome自定义标签,他们可能希望使用网页链接,而不是那种链接。实际上,可能唯一能够解释URI的应用程序就是github应用程序。
当您尝试启动任何应用无法处理的活动的意图时,标准android行为就会崩溃。
您可能能够利用PackageManager API来检测是否有人可以处理该意图。
| 归档时间: |
|
| 查看次数: |
2115 次 |
| 最近记录: |