Intent
- Activityから他Activityへ画面遷移するとき等に使います。(明示的Intent)
- いろんな情報を持たせて他Activityや他アプリと連携を渡したりもできます。(暗黙的Intent)
以下ソースでは明示的Intent,暗黙的Intentの主なものをまとめて実装しています
![]() |
| 図1 画面遷移前 |
![]() |
| 図2 画面遷移後 |
![]() |
| 図3 Intent連携 電話 |
![]() |
| 図4 Intent連携 SMS |
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_00 = (Button)findViewById(R.id.button_00);
button_00.setOnClickListener(this);
Button button_01 = (Button)findViewById(R.id.button_01);
button_01.setOnClickListener(this);
Button button_02 = (Button)findViewById(R.id.button_02);
button_02.setOnClickListener(this);
Button button_03 = (Button)findViewById(R.id.button_03);
button_03.setOnClickListener(this);
Button button_04 = (Button)findViewById(R.id.button_04);
button_04.setOnClickListener(this);
Button button_05 = (Button)findViewById(R.id.button_05);
button_05.setOnClickListener(this);
Button button_06 = (Button)findViewById(R.id.button_06);
button_06.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
Intent intent = null;
switch (v.getId()) {
case (R.id.button_00)://明示的Intent 画面遷移
intent = new Intent();
intent.setClass(MainActivity.this,SubActivity.class);
intent.putExtra("key", "画面遷移しました");//putExtra(key,送信したいtext)
//元々のアクティビティを破棄するなら
//finish();
break;
case (R.id.button_01)://電話
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:123456789"));
break;
case (R.id.button_02)://メール
intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "メールタイトルです");
intent.putExtra(Intent.EXTRA_TEXT, "ここは本文です");
break;
case (R.id.button_03)://ブラウザ
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://bodony-android.blogspot.com/"));
break;
case (R.id.button_04)://マップ
intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("geo:0,0"));
break;
case (R.id.button_05)://文字列送信、twitter,FaceBook,SMSの投稿等
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "ここに送信したいテキストですよ");
break;
case (R.id.button_06)://ギャラリー
intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
break;
}
if (intent != null){
startActivityForResult(intent, 0);//開始
}
}
}
SubActivity.javapublic class SubActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub);
Intent intent = getIntent();
if(intent != null){
String str = intent.getStringExtra("key");
TextView textview = (TextView)findViewById(R.id.textView5);
textview.setText(str);
}
}
}
res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="明示的Intent" />
<Button
android:id="@+id/button_00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="画面遷移" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暗黙的Intent" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow >
<Button
android:id="@+id/button_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="電話" />
<Button
android:id="@+id/button_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="メール" />
</TableRow>
<TableRow >
<Button
android:id="@+id/button_03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ブラウザ" />
<Button
android:id="@+id/button_04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="マップ" />
</TableRow>
<TableRow >
<Button
android:id="@+id/button_05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="文字列送信" />
<Button
android:id="@+id/button_06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ギャラリー" />
</TableRow>
</TableLayout>
</LinearLayout>
res/layout/activity_sub.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
res/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sample.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="sample.helloworld.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//画面遷移でActivityを追加する場合は必ずマニフェストにも追加
<activity android:name=".SubActivity"></activity>
</application>
</manifest>




0 件のコメント:
コメントを投稿