標籤:android blog http java 使用 strong
在一個Android應用中,主要是由一些組件組成,(Activity,Service,ContentProvider,etc.)在這些組件之間的通訊中,由Intent協助完成。
正如網上一些人解析所說,Intent負責對應用中一次操作的動作、動作涉及資料、附加資料進行描述,Android則根據此Intent的描述,負責找到對應的組件,將 Intent傳遞給調用的組件,並完成組件的調用。Intent在這裡起著實現調用者與被調用者之間的解耦作用。
Intent傳遞過程中,要找到目標消費者(另一個Activity,IntentReceiver或Service),也就是Intent的響應者,有兩種方法來匹配:
1,顯示匹配(Explicit):
public TestB extents Activity
{
.........
};
public class Test extends Activity
{
......
public void switchActivity()
{
Intent i = new Intent(Test.this, TestB.class);
this.startActivity(i);
}
}
代碼簡潔明了,執行了switchActivity()函數,就會馬上跳轉到名為TestB的Activity中。
2,隱式匹配(Implicit):
隱式匹配,首先要匹配Intent的幾項值:Action, Category, Data/Type,Component
如果填寫了Componet就是上例中的Test.class)這就形成了顯示匹配。所以此部分只講前幾種匹配。匹配規則為最大匹配規則,
1,如果你填寫了Action,如果有一個程式的Manifest.xml中的某一個Activity的IntentFilter段中定義了包含了相同的Action那麼這個Intent就與這個目標Action匹配,如果這個Filter段中沒有定義Type,Category,那麼這個Activity就匹配了。但是如果手機中有兩個以上的程式匹配,那麼就會彈出一個對話可框來提示說明。
Action的值在Android中有很多預定義,如果你想直接轉到你自己定義的Intent接收者,你可以在接收者的IntentFilter中加入一個自訂的Action值(同時要設定Category值為"android.intent.category.DEFAULT"),在你的Intent中設定該值為Intent的Action,就直接能跳轉到你自己的Intent接收者中。因為這個Action在系統中是唯一的。
2,data/type,你可以用Uri來做為data,比如Uri uri = Uri.parse(http://www.google.com);
Intent i = new Intent(Intent.ACTION_VIEW,uri);手機的Intent分發過程中,會根據http://www.google.com 的scheme判斷出資料類型type
手機的Brower則能匹配它,在Brower的Manifest.xml中的IntenFilter中首先有ACTION_VIEW Action,也能處理http:的type,
3,至於分類Category,一般不要去在Intent中設定它,如果你寫Intent的接收者,就在Manifest.xml的Activity的IntentFilter中包含android.category.DEFAULT,這樣所有不設定Category(Intent.addCategory(String c);)的Intent都會與這個Category匹配。
4,extras(附加資訊),是其它所有附加資訊的集合。使用extras可以為組件提供擴充資訊,比如,如果要執行“寄送電子郵件”這個動作,可以將電子郵件的標題、本文等儲存在extras裡,傳給電子郵件發送組件。
三,例子代碼:
public class HelloActivity extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
menu.add(0, Menu.FIRST+1, 1, R.string.menu_open);
menu.add(0, Menu.FIRST+2, 2, R.string.menu_edit);
menu.add(0, Menu.FIRST+3, 3, R.string.menu_update);
menu.add(0, Menu.FIRST+4, 4, R.string.menu_close);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case Menu.FIRST + 1:
{
this.setTitle("Open Text!");
Intent i = new Intent();
i.setAction("test_action");
if (Tools.isIntentAvailable(this,i))
this.startActivity(i);
else
this.setTitle("the Intent is unavailable!!!");
break;
}
case Menu.FIRST + 2:
{
this.setTitle("Edit Text!");
break;
}
case Menu.FIRST + 3:
{
this.setTitle("Update Text!");
break;
}
case Menu.FIRST + 4:
{
this.setTitle("Close Text!");
break;
}
}
return true;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
}
public class TestIntent extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Testing Intent here!");
this.setContentView(tv);
}
}
來看看TestIntent所在項目的Manifest.xml
....
<activity android:name="TestIntent" android:label="@string/hello"><intent-filter>
<action android:name="test_action"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
.....
四。用Intent調用系統中經常被用到的組件
此常用組件部分來源於(http://kuikui.javaeye.com/blog/318627)
1,web瀏覽器
Uri uri= Uri.parse("http://kuikui.javaeye.com");
returnIt = new Intent(Intent.ACTION_VIEW, uri);
2,地圖
Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);
3,調撥打到電話介面
Uri telUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);
4,直接撥打到電話
Uri callUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_CALL, callUri);
5,卸載
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
6,安裝
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
7,播放
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
8,掉用發郵件
Uri emailUri = Uri.parse("mailto:[email protected]");
returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);
9,發郵件
returnIt = new Intent(Intent.ACTION_SEND);
String[] tos = { "[email protected]" };
String[] ccs = { "[email protected]" };
returnIt.putExtra(Intent.EXTRA_EMAIL, tos);
returnIt.putExtra(Intent.EXTRA_CC, ccs);
returnIt.putExtra(Intent.EXTRA_TEXT, "body");
returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");
returnIt.setType("message/rfc882");
Intent.createChooser(returnIt, "Choose Email Client");
10,發簡訊
Uri smsUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_VIEW, smsUri);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.setType("vnd.android-dir/mms-sms");
11,直接發郵件
Uri smsToUri = Uri.parse("smsto://100861");
returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);
returnIt.putExtra("sms_body", "shenrenkui");
12,發多媒體訊息
Uri mmsUri = Uri.parse("content://media/external/images/media/23");
returnIt = new Intent(Intent.ACTION_SEND);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);
returnIt.setType("image/png");