Intentのandroidにおけるいくつかの使い方

6969 ワード

BroadcastReceiverから新しいActivityを起動する場合は、i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)を忘れないでください.
public class MyReceiver extends BroadcastReceiver{ 

public static final String action="acc";
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context,Receivered.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}


}


1.actionとtypeの指定
Intent importIntent = new Intent(Intent.ACTION_VIEW);
importIntent.setType("vnd.android.cursor.item/sim-contact");
importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
menu.add(0, 0, 0, R.string.importFromSim)
.setIcon(R.drawable.ic_menu_import_contact)
.setIntent(importIntent);

2.action,da ta,typeの指定
(1)暗黙的検索type
サンプルコード:
uri: content://simcontacts/simPeople/(id)
intent = new Intent("android.intent.action.SIMEDIT",uri);
startActivity(intent);


プログラムはdataのuriに基づいて一致するtypeを検索します(必要です)
providerのgetType()
case SIM_PEOPLE_ID:
return "vnd.android.cursor.item/sim-contact";
プロファイル内のfilter設定
AndroidManifest.xml







3.その他intentを設定する属性方式
Intent setComponent(ComponentName component)
Intent setClassName(Context packageContext, String className)
Intent setClassName(String packageName, String className)
Intent setClass(Context packageContext, Class> cls)
IntentはAndroidならではのものだと思います.Intentでプログラムが実行するアクション(view,edit,dialなど)と、プログラムがそのアクションを実行する際に必要な資料を指定できます.いずれも指定した後、startActivity()を呼び出すと、Androidシステムは指定した要求に最も合ったアプリケーションを自動的に探して実行します.
以下にいくつかのIntentの使い方を示します.
Webページを表示:
Uri uri = Uri.parse("http://www.google.com"); 
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);

地図を表示:
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
パス計画:
Uri uri = Uri.parse("http://maps.google.com/maps?f=dsaddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
電話番号:
ダイヤルアッププログラムを呼び出す
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse("tel.xxxxxx");
Intent it =new Intent(Intent.ACTION_CALL,uri);
これを使用するには、プロファイルに追加する必要があります.
SMS/MMSの送信
メッセージ送信プログラムを呼び出す
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
メールを送る
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
色とりどりの手紙を送る.
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra("sms_body", "some text");
it.putExtra(Intent.EXTRA_STREAM, uri);
it.setType("image/png");
startActivity(it);
Emailの送信
Uri uri = Uri.parse("mailto:[email protected]");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.setType("text/plain");
startActivity(Intent.createChooser(it, "Choose Email Client"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"[email protected]"};
String[] ccs={"[email protected]"};
it.putExtra(Intent.EXTRA_EMAIL, tos);
it.putExtra(Intent.EXTRA_CC, ccs);
it.putExtra(Intent.EXTRA_TEXT, "The email body text");
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.setType("message/rfc822");
startActivity(Intent.createChooser(it, "Choose Email Client"));
添付ファイルの追加
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"));
マルチメディアの再生
Intent it = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/song.mp3");
it.setDataAndType(uri, "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);
Uninstallプログラム
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
uninstall apk
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
install apk
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
play audio
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
//添付ファイルの送付
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/eoe.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
//検索アプリケーション
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
//指定されたアプリケーションの詳細ページが表示されます(これはサポートされていないようでapp_idが見つかりません)
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar