androidintent作用
⑴ Android Intent的作用,哪些類型的數據可以被傳遞
(1) 要激活一個新的Activity,或者讓一個現有的Activity執行新的操作,可以通過調用Context.startActivity()或者Activity.startActivityForResult()方法。這兩個方法需要傳入的Intent參數也稱為Activity Action Intent(活動行為意圖),根據Intent對象對目標Activity描述的不同,來啟動與之相匹配的Activity或傳遞信息。
(2) 要啟動一個新的服務,或者向一個已有的服務傳遞新的指令,調用Context.startService()方法或調用Context.bindService()方法將調用此方法的上下文對象與Service綁定。
Intent一旦發出,Android都會准確找到相匹配的一個或多個Activity、Service或Broadcast-Receiver作為響應。所以,不同類型的Intent消息不會出現重疊:BroadcastIntent消息只會發送給BroadcastReceiver,而絕不可能發送給Activity或Service。由startActivity()傳遞的消息也只可能發送給Activity,由startService()傳遞的Intent只可能發送給Service。
向下一個Activity傳遞數據(使用Bundle和Intent.putExtras)
Intent it = new Intent(Activity1.this, Activity2.class);
Bundle bundle=new Intent();
bundle.putString("name", "This is from MainActivity!");
it.putExtras("bd",bundle); // it.putExtra(「test」, "shuju」);
startActivity(it); // startActivityForResult(it,REQUEST_CODE);
⑵ android 裡面intent類干什麼的
intent即意圖
一:用來啟動其他新的Activity。
二:作為傳遞數據和事件的橋梁。傳遞數據時的代碼有兩種:
第一種是:
Intent
intent
=
new
Intent(CurrentActivity.this
,
OtherActivity.class);
intent.putExtra(「data」
,
somedata);
第二種是新建一個Bundle,再把該Bundle加入intent,如:
Bundle
bundle
=
new
Bundle()
;
bundle.putString(「data」
,
somedata)
;
intent.putExtras(bundle)。
⑶ android中intent的作用
意圖和意圖過濾器Intents and Intent Filters
一個應用程序的三個核心組件-活動,服務和廣播接收器是通過消息即意圖(Intents)來激活的。Intent息傳送是相同或不同應用中組件運行時晚綁定的一種機制。意圖本身,一個意圖對象,是一個包含被執行操作抽象描述的被動的數據結構-或者,對於廣播而言,是某件已經發生並被聲明的事情的描述。存在不同的機制來傳送意圖到每種組件中:
• 一個意圖對象是傳遞給Context.startActivity()或者Activity.startActivityForResult()來啟動一個活動或者讓一個存在的活動去做某些新的事情。
• 一個意圖對象是傳遞給Context.startService()來發起一個服務或者遞交新的指令給運行中的服務。類似的,一個意圖能被傳遞給Context.bindService() 來在調用組件和一個目標服務之間建立連接。作為一個可選項,它可以發起這個服務如果還沒運行的話。
• 傳遞給任意廣播方法(例如Context.sendBroadcast(),Context.sendOrderedBroadcast(), 或者Context.sendStickyBroadcast())的意圖對象被傳遞給所有感興趣的廣播接收者。許多種廣播產生於系統代碼。
在每個例子里,Android系統找到合適的活動,服務,或者一組廣播接收者來回應這個意圖,必要時實例化它們。這些消息傳送系統沒有重疊:廣播意圖僅被傳遞給廣播接收者,永遠不會給活動或者服務。一個傳送給startActivity()的意圖是只會被傳遞給一個活動,永遠不會給一個服務或廣播接收者,如此類推。
這篇文檔以意圖對象的描述開始,然後描述Android映射意圖到組件的規則-如何解決哪個組件應該接收一個意圖消息。對於沒有顯式命名一個目標組件的意圖,這個過程包括對照與潛在目標相關聯的意圖過濾器來測試這個意圖對象。
意圖對象Intent Objects
一個意圖Intent對象是一堆信息。它包含接收這個意圖的組件感興趣的信息(例如將要採取的動作和操作的數據)再加上Android系統感興趣的信息(例如應該處理這個意圖的組件類別和如何啟動一個目標活動的指令):
組件名稱Component name
應該處理這個意圖的組件名字. 這個欄位是一個ComponentName對象- 一個組合物:目標組件的完全合格的類名 (比如"com.example.project.app.FreneticActivity") 以及應用程序描述文件中設置的組件所在包的名字(比如, "com.example.project"). 這個組件名字的包部分和描述文件中設置的包名字不一定要匹配。
組件名字是可選的。如果被設置了,這個意圖對象將被傳遞到指定的類。如果沒有, Android使用另外的意圖對象中的信息去定位一個合適的目標- 請看本文稍後描述的意圖解析Intent Resolution。
組件名字通過如下方法:setComponent(),setClass(), 或者setClassName()設置並通過getComponent()讀取。
⑷ android開發中lntent與lntentFilter的作用
intent顧名思義,意圖。用於組件間通信。比如啟動一個activity,發送一個broadcast。
啟動activity的時候,在intent中添加內容,即可從當前類傳遞數據到一個activity中。
發送broadcast的時候,需要設置intent.setAction("意圖"),這里可以理解為發送一個什麼樣意圖的廣播。
sendBroadCast(intent);
在broadcastReceiver注冊時,就需要用到intentFilter,用於廣播接收的過濾。因為系統也會隨時發送許多廣播,你只想接收想要的廣播。
IntentFilter filter = new IntentFilter("some action");
registerBroadCastReceiver(receiver,filter);
⑸ Android開發之Intent.Action Android中Intent的各種常見作用【轉】
1 Intent.ACTION_MAIN
String: Android.intent.action.MAIN標識Activity為一個程序的開始。比較常用。Input:nothingOutput:nothing
2 Intent.Action_CALL
【直接呼叫,在6.0之後的版本需要獲取許可權,詳見 Android開發學習之路-Android6.0運行時許可權【轉】 】
Stirng: android.intent.action.CALL呼叫指定的電話號碼。Input:電話號碼。數據格式為:tel:+phone number Output:Nothing
Intent intent=new Intent(); intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);
3 Intent.Action.DIAL
String: action.intent.action.DIAL調用撥號面板
Intent intent=new Intent();intent.setAction(Intent.ACTION_DIAL); //android.intent.action.DIAL
intent.setData(Uri.parse("tel:1320010001");
startActivity(intent);
Input:電話號碼。數據格式為:tel:+phone number Output:Nothing說明:打開Android的撥號UI。如果沒有設置數據,則打開一個空的UI,如果設置數據,action.DIAL則通過調用getData()獲取電話號碼。但設置電話號碼的數據格式為 tel:+phone number.
4 Intent.Action.ALL_APPS
String: andriod.intent.action.ALL_APPS列出所有的應用。Input:Nothing.Output:Nothing.
5Intent.ACTION_ANSWER
Stirng:android.intent.action.ANSWER處理呼入的電話。Input:Nothing.Output:Nothing.
6 Intent.ACTION_ATTACH_DATA
String: android.action.ATTCH_DATA別用於指定一些數據應該附屬於一些其他的地方,例如,圖片數據應該附屬於聯系人Input: DataOutput:nothing
7 Intent.ACTION_BUG_REPORT
String: android.intent.action.BUG_REPORT顯示Dug報告。Input:nothingoutput:nothing
8 Intent.Action_CALL_BUTTON
String: android.action.intent.CALL_BUTTON.相當於用戶按下「撥號」鍵。經測試顯示的是「通話記錄」Input:nothingOutput:nothing
Intent intent = new Intent(Intent.ACTION_CALL_BUTTON);startActivity(intent);
9 Intent.ACTION_CHOOSER
String: android.intent.action.CHOOSER顯示一個activity選擇器,允許用戶在進程之前選擇他們想要的,與之對應的是Intent.ACTION_GET_CONTENT.
10. Intent.ACTION_GET_CONTENT
String: android.intent.action.GET_CONTENT允許用戶選擇特殊種類的數據,並返回(特殊種類的數據:照一張相片或錄一段音)
Input: TypeOutput:URI
int requestCode = 1001;Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // "android.intent.action.GET_CONTENT"
intent.setType("image/*"); // 查看類型,如果是其他類型,比如視頻則替換成 video/*,或 */*
Intent wrapperIntent = Intent.createChooser(intent, null);startActivityForResult(wrapperIntent, requestCode);
11 Intent.ACTION_VIEW
String:android.intent.action.VIEW用於顯示用戶的數據。比較通用,會根據用戶的數據類型打開相應的Activity。比如 tel:13400010001打開撥號程序,http://www.g.cn則會打開瀏覽器等。
Uri uri = Uri.parse("http://www.google.com"); //瀏覽器 Uri uri =Uri.parse("tel:1232333"); //撥號程序
Uri uri=Uri.parse("geo:39.899533,116.036476"); //打開地圖定位
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
//播放視頻
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/media.mp4");
intent.setDataAndType(uri, "video/*");
startActivity(intent);
//調用發送簡訊的程序
Intent it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "信息內容...");
it.setType("vnd.android-dirs-sms");
startActivity(it);
12 Intent.ACTION_SENDTO
String: android.intent.action.SENDTO
說明:發送簡訊息
//發送簡訊息 Uri uri = Uri.parse("smsto:13200100001");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "信息內容...");
startActivity(it);
//發送彩信,設備會提示選擇合適的程序發送 Uri uri = Uri.parse("content://media/external/images/media/23");
//設備中的資源(圖像或其他資源)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "內容");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(it);
//Email Intent intent=new Intent(Intent.ACTION_SEND);
String[] tos={"[email protected]"};
String[] ccs={"[email protected]"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
13 Intent.ACTION_GET_CONTENT
//選擇圖片 requestCode 返回的標識
Intent intent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
intent.setType(contentType); //查看類型 String IMAGE_UNSPECIFIED = "image/*";
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//添加音頻
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//拍攝視頻
int rationLimit = getVideoCaptureDurationLimit(); //SystemProperties.getInt("ro.media.enc.lprof.ration", 60);
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, sizeLimit);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, rationLimit);
startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
//視頻
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(contentType); //String VIDEO_UNSPECIFIED = "video/*";
Intent wrapperIntent = Intent.createChooser(intent, null);
((Activity) context).startActivityForResult(wrapperIntent, requestCode);
//錄音
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContentType.AUDIO_AMR); //String AUDIO_AMR = "audior";
intent.setClassName("com.android.soundrecorder",
"com.android.soundrecorder.SoundRecorder");
((Activity) context).startActivityForResult(intent, requestCode);
//拍照 REQUEST_CODE_TAKE_PICTURE 為返回的標識
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //"android.media.action.IMAGE_CAPTURE";
intent.putExtra(MediaStore.EXTRA_OUTPUT, Mms.ScrapSpace.CONTENT_URI); // output,Uri.parse("content:/s/scrapSpace");
startActivityForResult(intent, REQUEST_CODE_TAKE_PICTURE);
⑹ android中為什麼要使用intent進行通信
1、Intent對象詳解
Android的應用程序包含三種重要組件:Activity、Service、BroadcastReceiver,應用程序採用一致的方式來啟動它們----都是依靠Intent來進行啟動的,Intent就封裝了程序想要啟動程序的意圖,不僅如此,Intent還用於與被啟動組件進行交換信息。
組件類型
啟動方法
Activity
startActivity(Intent intent)
startActivityForResult(Intent intent,intrequestCode)
Service
ComponentName startService(Intent service)
boolean bindService(Intent service,ServiceConnection conn,int flags)
BroadcastReceiver
sendBroadcast(Intent intent)
sendBroadcast(Intent intent,String receiverPermission)
sendOrderedBroadcast(Intent intent,String receiverPermission,BroadcastReceiver resultReceiver,Handler scheler,int initialCode,String initialData,Bundle initialExtras)
sendOrderedBroadcast(Intent intent,String receiverPermission)
sendStickyOrderedBroadcast(Intent intent,BroadcastReceiver resultReceiver,Handler scheler,int initialCode,String initialData,Bundle initialExtras)
Intent對象大致包含Component、Action、Category、Data、Type、Extra和Flag這7種屬性,其中Component用於明確指定需要啟動的目標組件,而Extra則用於「攜帶」需要交換的數據。
2、Intent的屬性及Intent-filter配置
(1)Component屬性
Intent的Component屬性需要接受一個ComponentName對象,應用程序可根據給定的組件類去啟動特定的組件。
當程序通過Intent的Component屬性(明確指定了啟動哪個組件)啟動特定組件時,被啟動組件幾乎不需要使用<intent-filter.../>元素進行配置。
(2)Action、Category屬性與intent-filter配置
Intent的Action和Category屬性都是一個普通的字元串,其中Action代表該Intent所要完成的一個抽象「動作」,Category則用於為Action增加額外的附加類別信息,通常,Action與Category結合使用。
<intent-filter.../>元素里通常可包括如下子元素:
a、0~N個<action.../>子元素
b、0~N個<category.../>子元素
c、0~1個<data.../>子元素
<action.../><category.../>子元素的配置非常簡單,它們都可指定android:name屬性,該屬性的值就是一個普通字元串。
當<activity.../>元素里的<intent-filter.../>子元素里包含多個<action.../>子元素(相當於指定了多個字元串)時,就表明該Activity能響應Action屬性值為其中任意一個字元串的Intent。
一個Intent對象最多隻能包括一個Action屬性,程序調用Intent的setAction(String str)方法來設置Action屬性值;但一個Intent對象可包含多個Category屬性,調用Intent的addCategory(String str)方法添加。
當程序創建Intent時,該Intent默認啟動Category屬性值為Intent.CATEGORY_DEFAULT常量(常量值為android.intent.category.DEFAULT)的組件。
(3)指定Action、Category調用系統Activity
實際上,Android內部提供了大量標准Action、Category常量,其中用於啟動Activity的標准Action常量及對應的字元串如下:
Action常量
對應字元串
簡單說明
ACTION_MAIN
android.intent.action.MAIN
應用程序入口
ACTION_VIEW android.intent.action.VIEW 顯示指定數據
ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA 指定某塊數據將被附加到其它地方
ACTION_EDIT android.intent.action.EDIT 編輯指定數據
ACTION_PICK android.intent.action.PICK 從列表中選擇某項並返回所選的數據
ACTION_CHOOSER android.intent.action.CHOOSER 顯示一個Activity選擇器
ACTION_GET_CONTENT android.intent.action.GET_CONTENT 讓用戶選擇數據,並返回所選數據
ACTION_DIAL android.intent.action.DIAL 顯示撥號面板
ACTION_CALL android.intent.action.CALL 直接向指定用戶打電話
ACTION_SEND android.intent.action.SEND 向其他人發送數據
ACTION_SENDTO android.intent.action.SENDTO 向其他人發送消息
ACTION_ANSWER android.intent.action.ANSWER 應答電話
ACTION_INSERT android.intent.action.INSERT 插入數據
ACTION_DELETE android.intent.action.DELETE 刪除數據
ACTION_RUN android.intent.action.RUN 運行維護
ACTION_SYNC android.intent.action.SYNC 執行數據同步
ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY 用於選擇Activity
ACTION_SEARCH android.intent.action.SEARCH 執行搜索
ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH 執行Web搜索
ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST 工廠測試的入口點
標准Category常量及對應的字元串如下:
Category常量
對應字元串
簡單說明
CATEGORY_DEFAULT android.intent.category.DEFAULT 默認的Category
CATEGORY_BROWSABLE android.intent.category.BROWSABLE 指定該Activity能被瀏覽器安全調用
CATEGORY_TAB android.intent.category.TAB 指定Activity作為TabActivity的Tab頁
CATEGORY_LAUNCHER android.intent.category.LAUNCHER Activity顯示頂級程序列表中
CATEGORY_INFO android.intent.category.INFO 用於提供包信息
CATEGORY_HOME android.intent.category.HOME 設置該Activity隨系統啟動而運行
CATEGORY_PREFERENCE android.intent.category.PREFERENCE 該Activity是參數面板
CATEGORY_TEST android.intent.category.TEST 該Activity是一個測試
CATEGORY_CAR_DOCK android.intent.category.CAR_DOCK 指定手機被插入汽車底座(硬體)時運行該Activity
CATEGORY_DESK_DOCK android.intent.category.DESK_DOCK 指定手機被插入桌面底座(硬體)時運行該Activity
CATEGORY_CAR_MODE android.intent.category.CAR_MODE 設置該Activity可在車載環境下使用
3、Data、Type屬性與intent-filter配置
Data屬性通常用於向Action屬性提供操作的數據,Data屬性接收一個Uri對象,一個Uri對象通常通過如下形式的字元串表示:
content://com.android.contracts/contacts/1
tel:123
上面兩個字元串的冒號前面大致指定了數據的類型,冒號後面是數據部分。
Type屬性則用於明確指定Data屬性所指定數據的類型或MIME類型,當Intent不指定Data屬性時Type屬性才會起作用,否則Android系統將會根據Data屬性值來分析數據的類型,因此無需指定Type屬性。
一旦為Intent同時指定Action、Data屬性,那麼Android將可根據指定的數據類型來啟動特定的應用程序,並對指定數據執行相應的操作。下面介紹幾個Action、Data屬性的組合:
ACTION_VIEW content://com.android.contacts/contacts/1:顯示標識為1的聯系人的信息
ACTION_EDIT content://com.android.contacts/contacts/1:編輯標識為1的聯系人的信息
ACTION_DIAL content://com.android.contacts/contacts/1:顯示向標識為1的聯系人撥號的界面
ACTION_VIEW tel:123:顯示向指定號碼123撥號的界面
ACTION_DIAL tel:123:顯示向指定號碼123撥號的界面
ACTION_VIEW content://contacts/people/:顯示所有聯系人列表的信息,通過這種組合可以方便地查看系統聯系人
4、Extra屬性
Intent的Extra屬性通常用於在多個Action之間進行數據交換,Intent的Extra屬性值應該是一個Bundle對象,Bundle對象就像一個Map對象,它可以存入多組key-value對,這樣可以就可以通過Intent在不同Activity之間進行數據交換了。