1.Intent的用法:
(1) Action跳轉
1、 使用Action跳轉,當程序AndroidManifest.xml中某一個 Activity的IntentFilter定義了包含Action,如果恰好與目標Action匹配,且其IntentFilter中沒有定義其它的Type或Category過濾條件,那么就正好匹配了。如果手機中有兩個以上的Action程序匹配,那么就會彈出一個對話可框來提示說明。例如打開一個網址,彈出可選對話框:
Action 的值在Android中有很多預定義,如果想直接轉到自己定義的Intent接收者,可以在接收者的IntentFilter 中加入一個自定義的Action值(同時要設定 Category值為"android.intent.category.DEFAULT"),在你的Intent中設定該值為Intent的 Action就直接能跳轉到你自己的Intent接收者中,因為這個Action在系統中是唯一的。
intent.setClass(context, targetActivy. class ); // 或者直接用 Intent intent = new Intent(context, targetActivity.class);
1.從google搜索內容
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY,"searchString")
startActivity(intent);
2.瀏覽網頁
Uri uri = Uri.parse("http://www.google.com");
Intent it= new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
3.顯示地圖
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
4.路徑規劃
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);
5.撥打電話
Uri uri = Uri.parse("tel:xxxxxx");
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
6.調用發短信的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
7.發送短信
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
String body="this is sms demo";
Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, true);
mmsintent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, true);
startActivity(mmsintent);
8.發送彩信
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);
StringBuilder sb = new StringBuilder();
sb.append("file://");
sb.append(fd.getAbsoluteFile());
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mmsto", number, null));
// Below extra datas are all optional.
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_SUBJECT, subject);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_MESSAGE_BODY, body);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_CONTENT_URI, sb.toString());
intent.putExtra(Messaging.KEY_ACTION_SENDTO_COMPOSE_MODE, composeMode);
intent.putExtra(Messaging.KEY_ACTION_SENDTO_EXIT_ON_SENT, exitOnSent);
startActivity(intent);
9.發送Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
Intent it = new Intent(Intent.ACTION_SEND);
it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com");
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={"me@abc.com"};
String[] ccs={"you@abc.com"};
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"));
10.播放多媒體
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);
11.uninstall apk
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);
12.install apk
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
13. 打開照相機
<1>Intent i = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
this.sendBroadcast(i);
<2>long dateTaken = System.currentTimeMillis();
String name = createName(dateTaken) + ".jpg";
fileName = folder + name;
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, fileName);
values.put("_data", fileName);
values.put(Images.Media.PICASA_ID, fileName);
values.put(Images.Media.DISPLAY_NAME, fileName);
values.put(Images.Media.DESCRIPTION, fileName);
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileName);
Uri photoUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent inttPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
inttPhoto.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(inttPhoto, 10);
14.從gallery選取圖片
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, 11);
15. 打開錄音機
Intent mi = new Intent(Media.RECORD_SOUND_ACTION);
startActivity(mi);
16.顯示應用詳細列表
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
剛才找app id未果,結果發現用package name也可以
Uri uri = Uri.parse("market://details?id=<packagename>");
這個簡單多了
17尋找應用
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
18打開聯系人列表
<1>
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("vnd.android.cursor.item/phone");
startActivityForResult(i, REQUEST_TEXT);
<2>
Uri uri = Uri.parse("content://contacts/people");
Intent it = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(it, REQUEST_TEXT);
19 打開另一程序
Intent i = new Intent();
ComponentName cn = new ComponentName("com.yellowbook.android2",
"com.yellowbook.android2.AndroidSearch");
i.setComponent(cn);
i.setAction("android.intent.action.MAIN");
startActivityForResult(i, RESULT_OK);
Intent it = newIntent(Intent.ACTION_INSERT_OR_EDIT);
it.setType("vnd.android.cursor.item/contact");
//it.setType(Contacts.CONTENT_ITEM_TYPE);
it.putExtra("name","myName");
it.putExtra(android.provider.Contacts.Intents.Insert.COMPANY, "organization");
it.putExtra(android.provider.Contacts.Intents.Insert.EMAIL,"email");
it.putExtra(android.provider.Contacts.Intents.Insert.PHONE,"homePhone");
it.putExtra(android.provider.Contacts.Intents.Insert.SECONDARY_PHONE,
"mobilePhone");
it.putExtra( android.provider.Contacts.Intents.Insert.TERTIARY_PHONE,
"workPhone");
it.putExtra(android.provider.Contacts.Intents.Insert.JOB_TITLE,"title");
startActivity(it);
21.調用系統編輯添加聯系人(全有效):
Intent intent = newIntent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.NAME, "My Name");
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE,Contacts.PhonesColumns.TYPE_MOBILE);
intent.putExtra(Contacts.Intents.Insert.EMAIL, "com@com.com");
intent.putExtra(Contacts.Intents.Insert.EMAIL_TYPE, Contacts.ContactMethodsColumns.TYPE_WORK);
startActivity(intent);
intent action大全:
- android.intent.action.ALL_APPS
- android.intent.action.ANSWER
- android.intent.action.ATTACH_DATA
- android.intent.action.BUG_REPORT
- android.intent.action.CALL
- android.intent.action.CALL_BUTTON
- android.intent.action.CHOOSER
- android.intent.action.CREATE_LIVE_FOLDER
- android.intent.action.CREATE_SHORTCUT
- android.intent.action.DELETE
- android.intent.action.DIAL
- android.intent.action.EDIT
- android.intent.action.GET_CONTENT
- android.intent.action.INSERT
- android.intent.action.INSERT_OR_EDIT
- android.intent.action.MAIN
- android.intent.action.MEDIA_SEARCH
- android.intent.action.PICK
- android.intent.action.PICK_ACTIVITY
- android.intent.action.RINGTONE_PICKER
- android.intent.action.RUN
- android.intent.action.SEARCH
- android.intent.action.SEARCH_LONG_PRESS
- android.intent.action.SEND
- android.intent.action.SENDTO
- android.intent.action.SET_WALLPAPER
- android.intent.action.SYNC
- android.intent.action.SYSTEM_TUTORIAL
- android.intent.action.VIEW
- android.intent.action.VOICE_COMMAND
- android.intent.action.WEB_SEARCH
- android.net.wifi.PICK_WIFI_NETWORK
- android.settings.AIRPLANE_MODE_SETTINGS
- android.settings.APN_SETTINGS
- android.settings.APPLICATION_DEVELOPMENT_SETTINGS
- android.settings.APPLICATION_SETTINGS
- android.settings.BLUETOOTH_SETTINGS
- android.settings.DATA_ROAMING_SETTINGS
- android.settings.DATE_SETTINGS
- android.settings.DISPLAY_SETTINGS
- android.settings.INPUT_METHOD_SETTINGS
- android.settings.INTERNAL_STORAGE_SETTINGS
- android.settings.LOCALE_SETTINGS
- android.settings.LOCATION_SOURCE_SETTINGS
- android.settings.MANAGE_APPLICATIONS_SETTINGS
- android.settings.MEMORY_CARD_SETTINGS
- android.settings.NETWORK_OPERATOR_SETTINGS
- android.settings.QUICK_LAUNCH_SETTINGS
- android.settings.SECURITY_SETTINGS
- android.settings.SETTINGS
- android.settings.SOUND_SETTINGS
- android.settings.SYNC_SETTINGS
- android.settings.USER_DICTIONARY_SETTINGS
- android.settings.WIFI_IP_SETTINGS
- android.settings.WIFI_SETTINGS
- android.settings.WIRELESS_SETTINGS
附錄:
String |
"android.intent.action.ADD_SHORTCUT" |
動作:在系統中添加一個快捷方式。. |
String |
"android.intent.action.ALL_APPS" |
動作:列舉所有可用的應用。 |
String |
"android.intent.action.ANSWER" |
動作:處理撥入的電話。 |
String |
"android.intent.action.BUG_REPORT" |
動作:顯示 activity報告錯誤。 |
String |
"android.intent.action.CALL" |
動作:撥打電話,被呼叫的聯系人在數據中指定。 |
String |
"android.intent.action.CLEAR_CREDENTIALS" |
動作:清除登陸憑證 (credential)。 |
String |
"android.intent.action.VIEW" |
動作:和 VIEW_ACTION相同,是在數據上執行的標準動作。 |
String |
"android.intent.action.DELETE" |
動作:從容器中刪除給定的數據。 |
String |
"android.intent.action.DIAL" |
動作:撥打數據中指定的電話號碼。 |
String |
"android.intent.action.EDIT" |
動作:為制定的數據顯示可編輯界面。 |
String |
"android.intent.action.EMERGENCY_DIAL" |
動作:撥打緊急電話號碼。 |
String |
"android.intent.action.LOGIN" |
動作:獲取登錄憑證。 |
String |
" android.intent.action.MAIN " |
動作:作為主入口點啟動,不需要數據。 |
String |
"android.intent.action.PICK" |
動作:從數據中選擇一個項目item,將被選中的項目返回。 |
String |
"android.intent.action.PICK_ACTIVITY" |
動作:選擇一個activity,返回被選擇的activity的類名 |
String |
"android.intent.action.RUN" |
動作:運行數據(指定的應用),無論它(應用)是什么。 |
String |
"android.intent.action.SENDTO" |
動作:向 data指定的接收者發送一個消息。 |
String |
"android.intent.action.GET_CONTENT" |
動作:讓用戶選擇數據并返回。 |
String |
"android.intent.action.INSERT" |
動作:在容器中插入一個空項 (item)。 |
String |
"android.intent.action.SETTINGS" |
動作:顯示系統設置。輸入:無。 |
String |
" android.intent.action.VIEW " |
動作:向用戶顯示數據。 |
String |
"android.intent.action.WALLPAPER_SETTINGS" |
動作:顯示選擇墻紙的設置界面。輸入:無。 |
String |
"android.intent.action.WEB_SEARCH" |
動作:執行 web搜索。 |
String |
"android.intent.action.SYNC" |
動作:執行數據同步。 |
String |
"android.intent.action.SERVICE_STATE" |
廣播:電話服務的狀態已經改變。 |
String |
"android.intent.action.TIMEZONE_CHANGED" |
廣播:時區已經改變。 |
String |
"android.intent.action.TIME_SET" |
廣播:時間已經改變(重新設置)。 |
String |
"android.intent.action.TIME_TICK" |
廣播:當前時間已經變化(正常的時間流逝)。 |
String |
"android.intent.action.UMS_CONNECTED" |
廣播:設備進入 USB大容量存儲模式。 |
String |
"android.intent.action.UMS_DISCONNECTED" |
廣播:設備從 USB大容量存儲模式退出。 |
String |
"android.intent.action.WALLPAPER_CHANGED" |
廣播:系統的墻紙已經改變。 |
String |
"android.intent.action.XMPP_CONNECTED" |
廣播:XMPP連接已經被建立。 |
String |
"android.intent.action.XMPP_DI |
廣播:XMPP連接已經被斷開。 |
String |
"android.intent.action.SIG_STR" |
廣播:電話的信號強度已經改變。 |
String |
"android.intent.action.BATTERY_CHANGED" |
廣播:充電狀態,或者電池的電量發生變化。 |
String |
" android.intent.action.BOOT_COMPLETED " |
廣播:在系統啟動后,這個動作被廣播一次(只有一次) |
String |
"android.intent.action.DATA_ACTIVITY" |
廣播:電話的數據活動(data activity)狀態已經改變 |
String |
"android.intent.action.DATA_STATE" |
廣播:電話的數據連接狀態已經改變。 |
String |
"android.intent.action.DATE_CHANGED" |
廣播:日期被改變。 |
String |
"android.server.checkin.FOTA_CANCEL" |
廣播:取消所有被掛起的 (pending)更新下載。 |
String |
"android.server.checkin.FOTA_INSTALL" |
廣播:更新已經被確認,馬上就要開始安裝。 |
String |
"android.server.checkin.FOTA_READY" |
廣播:更新已經被下載,可以開始安裝。 |
String |
"android.server.checkin.FOTA_RESTART" |
廣播:恢復已經停止的更新下載。 |
String |
"android.server.checkin.FOTA_UPDATE" |
廣播:通過 OTA下載并安裝操作系統更新。 |
String |
"android.intent.action.MEDIABUTTON" |
廣播:用戶按下了“Media Button”。 |
String |
"android.intent.action.MEDIA_BAD_REMOVAL" |
廣播:擴展卡從SD卡插槽拔出,但是掛載點還沒unmount。 |
String |
"android.intent.action.MEDIA_EJECT" |
廣播:用戶想要移除擴展介質(拔掉擴展卡)。 |
String |
"android.intent.action.MEDIA_MOUNTED" |
廣播:擴展介質被插入,而且已經被掛載。 |
String |
"android.intent.action.MEDIA_REMOVED" |
廣播:擴展介質被移除。 |
String |
"android.intent.action.MEDIA_SCANNER_FINISHED" |
廣播:已經掃描完介質的一個目錄。 |
String |
"android.intent.action.MEDIA_SCANNER_STARTED" |
廣播:開始掃描介質的一個目錄。 |
String |
"android.intent.action.MEDIA_SHARED" |
廣播:擴展介質的掛載被解除 (unmount) |
String |
" android.intent.action.MEDIA_UNMOUNTED " |
廣播:擴展介質存在,但是還沒有被掛載 (mount)。 |
String |
"android.intent.action.MWI" |
廣播:電話的消息等待(語音郵件)狀態已經改變。 |
String |
" android.intent.action.PACKAGE_ADDED " |
廣播:設備上新安裝了一個應用程序包。 |
String |
" android.intent.action.PACKAGE_REMOVED " |
廣播:設備上刪除了一個應用程序包。 |
String |
"android.intent.action.PHONE_STATE" |
廣播:電話狀態已經改變。 |
String |
"android.intent.action.PROVIDER_CHANGED" |
廣播:更新將要(真正)被安裝。 |
String |
"android.intent.action.PROVISIONING_CHECK" |
廣播:要求provisioning service下載最新的設置 |
String |
"android.intent.action.SCREEN_OFF" |
廣播:屏幕被關閉。 |
String |
"android.intent.action.SCREEN_ON" |
廣播:屏幕已經被打開。 |
String |
"android.intent.action.NETWORK_TICKLE_RECEIVED" |
廣播:設備收到了新的網絡 "tickle"通知。 |
String |
"android.intent.action.STATISTICS_REPORT" |
廣播:要求 receivers報告自己的統計信息。 |
String |
"android.intent.action.STATISTICS_STATE_CHANGED" |
廣播:統計信息服務的狀態已經改變。 |
String |
"android.intent.action.CFF" |
廣播:語音電話的呼叫轉移狀態已經改變。 |
String |
"android.intent.action.CONFIGURATION_CHANGED" |
廣播:設備的配置信息已經改變,參見 Resources.Configuration |
String |
"android.intent.category.ALTERNATIVE" |
類別:說明activity是用戶正在瀏的數據的一個可選操作。 |
String |
"android.intent.category.WALLPAPER" |
類別:這個 activity能過為設備設置墻紙。 |
String |
"android.intent.category.UNIT_TEST" |
類別:應該被用作單元測試(通過 test harness運行)。 |
String |
"android.intent.category.TEST" |
類別:作為測試目的使用,不是正常的用戶體驗的一部分。 |
String |
"android.intent.category.TAB" |
類別:activity應該在TabActivity中作為一個tab使用 |
String |
"android.intent.category.SAMPLE_CODE" |
類別:To be used as an sample code example (not part of the normal user experience). |
String |
"android.intent.category.PREFERENCE" |
類別:activity是一個設置面板 (preference panel)。 |
String |
"android.intent.category.HOME" |
類別:主屏幕 (activity),設備啟動后顯示的第一個 activity。 |
String |
"android.intent.category.BROWSABLE" |
類別:能夠被瀏覽器安全使用的 activities必須支持這個類別。 |
String |
"android.intent.category.DEFAULT" |
類別:如果 activity是對數據執行確省動作(點擊, center press)的一個選項,需要設置這個類別。 |
String |
"android.intent.category.DEVELOPMENT_PREFERENCE" |
類別:說明 activity是一個設置面板 (development preference panel). |
String |
"android.intent.category.EMBED" |
類別:能夠在上級(父)activity中運行。 |
String |
"android.intent.category.FRAMEWORK_INSTRUMENTATION_TEST" |
類別:To be used as code under test for framework instrumentation tests. |
String |
"android.intent.category.GADGET" |
類別:這個 activity可以被嵌入宿主 activity (activity that is hosting gadgets)。 |
String |
"android.intent.category.LAUNCHER" |
類別:Activity應該被顯示在頂級的 launcher 中。 |
String |
"android.intent.category.SELECTED_ALTERNATIVE" |
類別:對于被用戶選中的數據,activity是它的一個可選操作。 |
int |
8 0x00000008 |
啟動標記:和 NEW_TASK_LAUNCH聯合使用,禁止將已有的任務改變為前景任務 (foreground)。 |
int |
4 0x00000004 |
啟動標記:設置以后,activity將成為歷史堆棧中的第一個新任務(棧頂)。 |
int |
1 0x00000001 |
啟動標記:設置以后,新的 activity不會被保存在歷史堆棧中。 |
int |
2 0x00000002 |
啟動標記:設置以后,如果 activity已經啟動,而且位于歷史堆棧的頂端,將不再啟動(不重新啟動) activity。 |
int |
16 0x00000010 |
啟動標記:如果這個標記被設置,而且被一個已經存在的 activity用來啟動新的 activity,已有 activity 的回復目標 (reply target) 會被轉移給新的 activity。 |
String |
"android.intent.extra.INTENT" |
附加數據:和 PICK_ACTIVITY_ACTION一起使用時,說明用戶選擇的用來顯示的 activity;和 ADD_SHORTCUT_ACTION 一起使用的時候,描述要添加的快捷方式。 |
String |
"android.intent.extra.LABEL" |
附加數據:大寫字母開頭的字符標簽,和 ADD_SHORTCUT_ACTION一起使用。 |
String |
"android.intent.extra.TEMPLATE" |
附加數據:新記錄的初始化模板。 |
Creator |
無 |
無 |
參考推薦:
intent
(google)
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
