Chrome Custom Tabs を とりあえず数行で使う

スマホでブラウザを起動する, っての最近では面倒.

LINEとかメッセージでリンクを送ってくる人とか.

20150910-215123

さりげなく開かずにテキトーな会話を続けたりします.

考えてみると「なんかブラウザって重いしトロい」から.

そこで, 最近登場したのが「Chrome Custom Tabs」というしくみ.

Chrome Custom Tabs - Google Chrome

performance

表示までが非常に高速化されてるようにに見える.

実装しようとサンプルをダウンロードしてみる.

GoogleChrome/custom-tabs-client

アプリ内モジュールやヘルパーやプロセス間の通信など
依存関係があるのでわかりづらい.

とりあえずシンプルにURLを渡すだけでで起動してみたいだけなので以下で.

build.gradle

compile 'com.android.support:customtabs:23.0.1'

サンプル内モジュール customtabs 以下は不要.

コピペするクラス

CustomTabsHelper
KeepAliveService
CustomTabActivityHelper
WebViewFallback

shared というモジュールは作らずベタクラスとしてコピペ.
アプリ内モジュール依存はなしで.

起動

...
Uri uri = Uri.parse("http://google.com/");
CustomTabsIntent customTabsIntent =
    new CustomTabsIntent.Builder().build();
CustomTabActivityHelper
    .openCustomTab(this, customTabsIntent, uri, new WebViewFallback());
...

UIカスタマイズはしない. するならここで.

WebViewFallback#openUri()

    ...
    //Intent intent = new Intent(activity, WebViewActivity.class);
    //intent.putExtra(WebActivity.EXTRA_URL, uri.toString());
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(uri);
    activity.startActivity(intent);
    ...

Chromeアプリ (stable/beta/dev) をインストールしてない場合は,
デフォルトブラウザなどに暗黙的に投げる.

で ?

起動はできる.
が, これだけでは恩恵にありつけない.

20150910-224352

サイト内検索などはできるようにはなるが
「なんかブラウザって重いしトロい」が解消せず.

以下の機能があるという.

Pre-warming of the Browser in the background, while avoiding stealing resources from the application.

Providing a likely URL in advance to the browser, which may perform speculative work, speeding up page load time.

これを使わないと意味ないな...

(つづく)


Google (Now?) からの wikipedia の「URLを開くアプリが見つかりません」

ここ2-3日きっとみんなも遭遇してるはず.

Google Now から, 何か検索したときの Wikipedia ページへのリンクタップでのこれ.

20150825-040622

たくさん困ってる人がいる模様.

検索機能に問題は無いが特定サイトへ飛ぼうとすると「URLを開くアプリが見つかりません」のメッセージが出てリンク先が開けない Wikipediaとnaverまとめが開けないのは検索エンジンとして致命的です

昨日から突然Wikipediaが閲覧不可になりました。タイトルにあるように、「URLを開くアプリが見つかりません」と出てくるのでどうすれば良いのか困っています。

昨日から突然Wikipediaが閲覧不可… │ au Q&Aプラス

タップしたときのログ.

...
I/ContextIntentStarter(19425): No activity found for Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=http://ja.m.wikipedia.org/wiki/メインページ flg=0x8080000 pkg=org.wikipedia (has extras) }
E/ContextIntentStarter(19425): No activity found for any of the 1 intents
...

「pkg=org.wikipedia」とあるのでアプリ指定. このアプリをインストールしておけばアプリがタップ時に起動する.

ウィキペディアモバイル - Google Play の Android アプリ

インストールしてなければ「URLを開くアプリがみつかりません」となる.

すこし待てば直るのかな. wikipedia 入れるしかないのかな.

Google - Google Play の Android アプリ

Google Nowランチャー - Google Play の Android アプリ


onActivityResult() を透過的に Fragment に渡す

Activity内のFragmentに, 別Activityからの結果を渡す.

...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Fragment fragment = getFragmentManager().findFragmentById(R.id.your_fragment_id);
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}
...

fragment いなかったら正しく fragment.setArguments(bundle); で.