他のアプリから呼び出せるアプリを作る
Making Application Callable by Others
開発しているアプリで使っているIntentオブジェクトを
呼び出すのを他のアプリに割り当てるにはどうしたら?
この解決法を考えていきます。
以下のように開発しているアプリにクラスがあると想定します。
package net.learn2develop.intentfilters;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MyBrowserActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreat(savedInstanceState);
setContentView(R.layout.browser);
Uri url = getIntent().getData();
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new Callback());
if (url!=null){
webView.loadUrl(url.toString());
}else{
webView.loadUrl("http://www.google.com");
}
}
private class Callback extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(
WebView view, String url){
return(false);
}
}
}
基本的には、上のコードにあるクラスはbrowser.xmlの名前の
WebViewを使ったXMLファイルをロードしておきます。
このコードで、Intentオブジェクトを通じて渡されたデータを元に、
ウェブページが表示されます。
WebViewを使ったbrowser.xmlは以下の様になります。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
<android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout>
AndroidManifest.xmlファイルにもこのクラスを宣言しておきます。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.intentfilters"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk
android:minSdkVersion="8"
androi:targetSdkVersion="15"
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MyBrowserActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW />
<action android:name="net.learn2develop.MyBrowser" />
<category android:name="android.intent.category.DEFAULT />
<data android:scheme="http" />
</intent-filter>
</activity>
</application>
</manifest>
上に記述した要素をMyBrowserActivityクラスへの<intent-filter>
要素 に格納させています。
この<intent-filter>には
android.intent.action.VIEWとnet.learn2develop.MyBrowserの
2つのactionがあります。
android.intent.action.VIEW定数を使うことでアクションを呼び出し、
net.learn2develop.MyBrowserの名前を使って直接呼び出すことが
できます。
この動作はhttp:のデータスキームも指定します。
このアクティビティはhttp:を頭に付けてアクションに
必要なデータを渡す事を意味しています。
これによりhttp://www.google.comのように指定できます。
MyBrowseActivityクラスを実行するためには、
メインのアクティビティに以下のようなステートメントを
使うこともできます。
package net.learn2develop.intentfilter;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layour.activity_main);
Intent i = new Intent("net.learn2develop.MyBrowser");
i.setData(Uri.parse("http://www.amazon.com"));
startActivity(i);
}
}
これでAmazon.comのサイトを画面に表示させられます。
この場合、net.lear2develop.MyBrowserアクションを使って
MyBrowserActivityを直接呼び出しています。
代わりの方法として、android.content.Intent.ACTION_VIEW定数を
MyBrowserActivityを呼び出すのに使えます。
これはandroid.intent.action.VIEWと同じ値として評価してます。
しかし、今回はアクションを完了させるため
選んだアプリケーションに要求してダイヤログを確認できるようになります。
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.amazon.com"));
atartActivity(i);
これで複数のアプリケーションでこのアクションを
処理できるようになります。
そしてAndroidManifest.xmlファイルに戻り、<data>要素を
修正します。
<activity
android:name=".MyBrowserActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="net.learn2develop.MyBrowser" />
<categoty android:name="android.intent.category.DEFAULT" />
<!--
<data android:scheme="http" />
-->
<data android.mimeType="text/html" />
</intent-filter>
</activity>
この場合の<data>要素は、アクティビティのMIMEメディアタイプを
指定して、処理できるようにしています。
このMIMEタイプでアクティビティを呼び出すには、
setType()メソッドを使う必要があります。
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
//---if your are using setType(), no need to use setData()---
//i.setData(Uri.parse("http://www.amazon.com"));
//---indicates the type that the target activity will handle---
i.setType("text/html");
i.putExtra("URL", "http://www.amazon.com");
startActivity(i);
setType()メソッドがsetData()メソッドを使ってセットすることで、
自動的にすべてのデータをクリアすることに注意します。
このため、setData()メソッドを使う必要はなくなります。
この場合、アクティビティにデータを渡すには、
putExtra()メソッドを使います。
上のコード部は、開発者が指定したMIMEタイプを処理できる
アプリケーションのリストを示してくれます。
putExtra()メソッドを使って渡したデータを検索するためには、
getStringExtra()メソッドを使うことができます。
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
//Uri url = getIntent().getData();
Uri url = Uri.parse(getIntent().getStringExtra("URL"));
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new Callback());
if(url!=null){
webView.loadUrl(url.toString());
}else{
webView.loadUrl("http;//www.google.com");
}
}
|
【用語解説】
XML
HTMLと同じような言語ですが、
すべての型の複雑なデータ構造を
送信するための設計ができる
ファイル型式。
オブジェクト
ある手続きを持った
データアイテムで、
他の手続きと関連づけされるもの
metaタグ
ウェブページに与える
情報の宣言につかわれ
検索エンジン・ウェブアプリ
などにウェブページの内容を
伝える役目をする。
metaタグの内容は
ページ内に表示されない。
コンストラクタ
オブジェクト指向
プログラミングで使われ
新しいオブジェクトが
作られ時に呼び出される
メソッドのこと。
クラス
オブジェクト指向
プログラミングでの
オブジェクトの型
オブジェクト指向プログラミング
プログラミングの
方式のことで、
プログラマはデータ型を
定義できるだけでなく
自動的にデータ型と
メソッドを関連付け
できる
アクティビティ
Androidにおける
アクティビティは
Androidアプリの画面に
相当するもの
ボタンやウェブページ
など表示されている
すべてのもののこと。
|