インテントとは、アプリケーションから、新しいアクティビティを起動するためのパラメータです。
起動するアクティビティは、同じアプリケーション内のアクティビティでも、他のアプリケーション内のアクティビティでも指定することができます。他のアプリケーションのアクティビティを指定するためには、アクションと、Uriという二つのパラメータを指定します。Androidでは、あらかじめ規定されたインテントにたいする動作が組み込まれています。
ここでは、代表的なアクションである、ACTION_VIEWに対する動作を確認するためのアプリケーションを作成してみましょう。
<?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>
<?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>
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(mButtonListener); }
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 | 動作 | 例 |
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 |
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()); } } }
#ref(): File not found: "インテントを発行してみよう.pdf" at page "勉強会/インテントを発行してみよう"