Intent(インテント)連携をまとめてみる
アクティビティ間の連携は、必ず使うことになるので。
明示的
起動したいアクティビティをパッケージ名とクラス名で直接指定する。
Intent intent = new Intent(helloworld.this,XXXXX.class); intent.setAction(Intent.ACTION_VIEW); startActivity(intent);
コマンドラインから
$ am start -n パッケージ名/.アクティビティクラス(com.suddenAngerSystem/.BroadCastIntentの様な)
暗黙的
起動したいアクティビティをインテントフィルタの属性とパラメータURIで指定する。
コマンドラインから
$ am start -a アクション名(android.intent.action.EDIT等) -d content://xxxxx(file:/xxやhttp://xxの様なタイプ://リソース) -t xxx/yyy(text/css等のMIME/TYPE)
ブラウザへ
Intent bi = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); startActivity(bi);
ダイアラーへ
Intent di = new Intent(Intent.ACTION_VIEW, Uri.parse("tel:123456789")); startActivity(di);
通話開始へ
Uri uri=Uri.parse("tel:117"); Intent intent=new Intent(Intent.ACTION_CALL,uri); startActivity(intent)
GoogleMapsへ
Intent mi = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=Tokyo")); startActivity(mi);
Google map で経路検索へ
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity"); intent.setData(Uri.parse("http://maps.google.com/maps?myl=saddr&daddr=&dirflg=d")); startActivity(intent);
コンタクトリストへ
Intent cti = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/1")); startActivity(cti);
メーラへ
Uri uri=Uri.parse("mailto:npaka@npaka.net"); Intent intent=new Intent(Intent.ACTION_SENDTO,uri); intent.putExtra(Intent.EXTRA_SUBJECT,"タイトル"); intent.putExtra(Intent.EXTRA_TEXT,"ボディのテキスト"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent);
添付ファイルをつけてメーラへ
Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_SUBJECT, “The email subject text”); it.putExtra(Intent.EXTRA_STREAM, “file:///sdcard/mysong.mp3”); sendIntent.setType(“audio/mp3”); startActivity(Intent.createChooser(it, “Choose Email Client”));
音楽プレーヤーへ
Uri uri = Uri.parse(“file:///sdcard/song.mp3”); Intent it = new Intent(Intent.ACTION_VIEW, uri); it.setType(“audio/mp3”); startActivity(it); Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, “1”); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
Android設定へ
Intent intent=new Intent("android.settings.SETTINGS"); startActivity(intent);
マーケットへ
Uri uri=Uri.parse("market://search?q=pname:net.npaka.actiongame"); Intent intent=new Intent(Intent.ACTION_VIEW,uri); startActivity(intent);
マーケットアプリ詳細情報へ
Uri uri = Uri.parse(“market://details?id=app_id”); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
アプリアンインストールへ
Uri uri=Uri.fromParts("package","net.npaka.helloworld",null); Intent intent=new Intent(Intent.ACTION_DELETE,uri); startActivity(intent);
manifest.xml
intent-filter要素で受け付けるアクション種別、カテゴリー、データ形式を宣言する。
<activity android:name=".ReTranslateActivity" android:icon="@drawable/icon" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.EDIT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/*" /> </intent-filter> <intent-filter> <action android:name="com.adamrocker.android.simeji.ACTION_INTERCEPT" /> <category android:name="com.adamrocker.android.simeji.REPLACE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
参考ページ
https://sites.google.com/a/gclue.jp/android-docs-2009/intent
http://d.hatena.ne.jp/mokkouyou/20100119/1263899123
http://blog.haw.co.jp/android/?p=54
http://www.次世代創造機構.jp/android/androidLecture/Intent/Intent.html
http://eguchi.jp/blognplus/index.php?e=409
http://blog.livedoor.jp/maochan__/archives/2194191.html
http://y-anz-m.blogspot.com/2009/06/intent.html
http://www.saturn.dti.ne.jp/~npaka/android/ActivityEx/index.html
http://www.次世代創造機構.jp/android/androidLecture/Intent2/Intent2.html
わかりやすいコードを引用してまとめる。
みなさまわかりやすいです。