[[勉強会]] * インテントを発行してみよう [#lb576677] インテントとは、アプリケーションから、新しいアクティビティを起動するためのパラメータです。 起動するアクティビティは、同じアプリケーション内のアクティビティでも、他のアプリケーション内のアクティビティでも指定することができます。他のアプリケーションのアクティビティを指定するためには、アクションと、Uriという二つのパラメータを指定します。Androidでは、あらかじめ規定されたインテントにたいする動作が組み込まれています。 ここでは、代表的なアクションである、ACTION_VIEWに対する動作を確認するためのアプリケーションを作成してみましょう。 ** プロジェクトを作成しましょう [#u24e2ce2] - "File -> New -> Project"を選択して、開いたダイアログで"Android Poroject"を選択して、"Next"ボタンを押します。 - 開いたダイアログで、以下のように入力してFinishボタンを押します。 -- Project Name: HelloIntent -- Package name: jp.hews.hellointent -- Activity Name: HelloIntentActivity -- Application Name: ハローインテント ** 表示文字列を定義する [#xe5e9d08] - res/values/strings.xmlを開いて、ボタンに表示するための文字列を定義しておきます。GUIでaddボタンを押してStringを選択して挿入する方法と、下部の「strings.xml」タブでXMLを表示して、入力する方法があります <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HelloIntent</string> <string name="app_name">ハローインテント</string> <!-- 押しボタンに表示する文字列 --> <string name="button_label">開始</string> </resources> ** 画面を定義する [#v8f3894b] - res/layout/main.xmlを編集して、画面を定義します。テキスト入力用のEditText, 開始ボタン、エラー表示用のTextViewを垂直に配置します。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/edit_text_id" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/button_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button_label" /> <TextView android:id="@+id/text_id" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> #ref(intent.001.main.png) ** ボタンにリスナーを登録します [#r4fd4044] - HelloIntentActivity.javaを開いて、onCreateメソッドでボタンを取り出して、リスナーを登録します。- リスナー内のonClickメソッドで次で定義するsendIntentメソッドを呼び出します。 - HelloIntentActivity.javaを開いて、onCreateメソッドでボタンを取り出して、リスナーを登録します。 - ボタンリスナーを作成して、リスナー内のonClickメソッドで次で定義するsendIntentメソッドを呼び出します。 - onClickListenerでリスナーを登録します。 private View.OnClickListener mButtonListener = new View.OnClickListener() { public void onClick(View v) { sendIntent(); } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button)findViewById(R.id.button_id); b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { sendIntent(); }}); b.setOnClickListener(mButtonListener); } ** インテントを作成して、発行する [#e36747e2] - EditTextの内容をUriに変換して、インテントを作成します。 - 作成したインテントを引数にしてstartActivityを呼び出します。 - エラー発生時は、TextViewにエラーを表示します。 private void sendIntent() { try { EditText et = (EditText)findViewById(R.id.edit_text_id); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(et.getText().toString())); startActivity(i); } catch (Exception e) { TextView t = (TextView)findViewById(R.id.text_id); t.setText(e.toString()); } } ** Uriを入力してみる [#x44fbe3e] - あらかじめ組み込まれているUriに対応するアクティビティを起動してみます。 |Uri|動作|例| |http://web_address |ブラウザを起動する|http://www.google.com/| |tel:phone_number|ダイヤル画面を表示します|tel:123456| |geo:latitude,longitude|地図を表示します|geo:0,0?q=Sapporo| |content://contacts/people|コンタクトリストを表示します|content://contacts/people/1| ** 地図を表示してみる [#h4537c94] - geo:0,0?q=Sapporo #ref(intent.002.Sapporo.png) ** まとめ [#g4eae58e] - インテントは、アクションと、Uriで指定します - startActivityを呼び出すと対応するアクティビティが起動します - Androidにはいくつかのインテントに対応するアクティビティがあらかじめ組み込まれています ** ソースコード [#e1cc3414] package jp.hews.hellointent; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class HelloIntentActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button)findViewById(R.id.button_id); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendIntent(); }}); } private void sendIntent() { try { EditText et = (EditText)findViewById(R.id.edit_text_id); Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(et.getText().toString())); startActivity(i); } catch (Exception e) { TextView t = (TextView)findViewById(R.id.text_id); t.setText(e.toString()); } } } ** プレゼンテーション資料 [#m302c589] #ref(インテントを発行してみよう.pdf)