Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android之Intent

android之Intent

編輯:關於Android編程

Intent是一個消息對象你可以用來去請求一個來自另外一個app component的action。雖然Intent便於在component之間通信,,到那時有三種基本的用例


1:開啟一個activity
a:startActivity
通過傳遞一個Intent給startActivity()函數,開啟一個Activity的新的實例,。Intent中描述新開啟的Activity以及帶有一些相關的數據
b:startActivityForResult
開啟一個新的Activity,當新開啟的Activity結束後,返回一定的數據到原始的Activity。


2:開啟一個service
service是不帶用戶界面的在背地裡運行的組件,比如說下載文件,播放音樂。
a: startService
開啟一個服務來執行一次操作,傳遞Intent給startService()函數
b:bindService
如果service被設計成服務器-客戶端的接口模式,可以從另外一個component綁定一個service。


3:傳遞一個broadcast
broadcast是一個任何app都可以接受的message。可以通過sendBroadcast(),sendOrderedBroadcast(),sendStickyBroadcast()來給其他app傳遞broadcast。


Intent的分類
1:顯式的Intent
通過指定一個component的全名來指定開啟的Intent。
2:隱式的Intent
不指定一個component的名字,只聲明它要做的action。
當創建一個隱式的Intent的時候,系統會通過比較manifest中的Intent-filter和Intent的內容,如果Intent的內容和Intent-filter中的相互匹配,系統就會開啟那個component來傳遞Intent,如果有多個Intent-filter匹配的話,系統就會彈出一個對話框讓調用者來選擇。


Intent負載的信息
Intent上面負載著系統決定啟動哪一個component(component的名字,component的category),以及接受的組件需要是用的信息等。
1:Component name
要啟動的component的名字,這個是可選的,對於顯式的就用名字,隱式的可以通過action,category,data等來判斷啟動哪一個component。
2:Action
指定要執行的動作
3:data
URI,或者MIME格式的對象。data是由Intent的Action決定的,如果Action是一個ACTION_EDIT,那麼data就應當包含編輯文檔的URI。
當創建一個Intent的時候,它往往是重要的去指定data的類型。指定data為MIME格式能夠輔助系統找到最合適的component來接受Intent,但是MIME有時候可以從URI中推斷出來,特別當data是一個content的時候。
setData(),setType(),setDataAndType來設置數據的格式。
4:category
給一個component提供附加信息,一個Intent中可以放置任意數目的category,但是大部分Intent並不需要一個category。
5:extras
就像一些action使用uri,一些action要使用extras來給Intent的action提供附加信息。
6:Flags
這個主要是處理指明系統如何去加載一個activity,例如把新啟動的activity加載到哪一個Task中,它加載完畢後怎麼處理它。
Intent例子
1:顯式調用
// Executed in an Activity, so 'this' is the Context
// The fileUrl is a string URL, such as "http://www.example.com/image.png"
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.setData(Uri.parse(fileUrl));
startService(downloadIntent);
2:隱式調用
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type


// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}


3:隱式調用,chooser
Intent intent = new Intent(Intent.ACTION_SEND);
...


// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);


// Verify the intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
}


4:manifest中設置intent-filter來選擇action
它格式是
action
data
category
如果接收的Intent是隱式的,那麼必須包含 CATEGORY_DEFAULT category









5:Intent-filter中選擇實例




























其中


這裡的 ACTION_MAIN action和CATEGORY_LAUNCHER category必須成對結合起來。
在ShareActivity中,兩個Intent-filter提供了關於send的action,當要share text的時候,這兩個都合適。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved