方式一: 使用Action
在我們開發Android App應用的時候,有些需求需要我們啓動其他的App來處理一些邏輯
例如我們需要根據一個地址來調用系統或者相關的地圖Map App,這樣我們不用在自己的App中編寫相應的功能,而是通過Intent來發送一些請求,調用相關的應用來處理這些請求。並且我們稱這種Intent爲隱式的Intent;這種隱式的Intent是相對於顯式的Intent來講的。顯式的Intent我們都比較熟悉,顯式的Intent常常需要聲明類的名稱,而隱式的Intent我們需要聲明一個Action,我們Action中定義了我們想要處理的請求。
與Action相關聯的還有data,例如我們需要查看的地址,或者我們需要撥打的電話號碼,或者我們需要發送郵件的郵件地址等等。
- 例如:
Uri number = Uri.parse("tel:10086");Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
上述代碼中,我們通過startActivity()來調用Phone App,並進行撥打(10086)的通話操作。當然這裏還有很多案例,這裏主要是從API 文檔中摘錄的了。貼在這裏供大家參考。
查看地圖:
// Map point based on address
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
// Or map point based on latitude/longitude
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14");
// z param is zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
瀏覽網頁:
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
發送郵件:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// The intent does not have a URI, so declare the "text/plain" MIME type
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); // recipients
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment"));
// You can also attach multiple items by passing an ArrayList of Uris
創建一個事件通知:
Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance().set(2012, 0, 19, 7, 30);
Calendar endTime = Calendar.getInstance().set(2012, 0, 19, 10, 30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, "Ninja class");
calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");
此外,我們在定義我們的Intent的時候,要儘可能的詳細,
例如我們希望調用系統的圖片查看器瀏覽圖片,我們應該定義MIME type" image/*."
,以防止會啓動map app進行查看。而且如果沒有app來響應我們的請求,我們的app就會崩潰。
因此爲了防止我們的app發送intent沒有其他app來響應而導致應用異常退出,我們在發送intent前進行驗證。爲了驗證是否有app的Activity來響應我們的Intent請求,我們需要調用queryIntentActivities()來進行驗證。
這個方法會返回一個list,我們通過判斷list是否爲空來驗證,這樣我們可以安全的使用Intent來實現不同App之間Activity的交互。如果沒有響應的Acitivty來響應,我們可以提供一些鏈接給用戶進行下載安裝。
- 驗證方法如下:
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
下面我們通過具體的案例來使用隱式的Intent,使不同的App中的Activity進行交互。
首先我們創建第一個項目appsend,我們創建一個按鈕,並且在點擊事件中創建Intent,並設置Action和type,並添加onActivityResult()來接收我們從第二個應用中返回的數據。
public void button(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setType("test/");
startActivityForResult(intent, 2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 2:
switch (resultCode) {
case Activity.RESULT_OK:
text.setText("URI:" + data.getDataString());
break;
}
break;
}
}
- 第二步:我們創建第二個應用,並且在清單文件中進行配置Intent-filter;
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="test/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
- 第三步:在第二個Activity中監聽返回按鈕,並傳回數據。
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
Intent result = new Intent("com.example.appsend",
Uri.parse("content://result_uri"));
setResult(Activity.RESULT_OK, result);
finish();
break;
}
return super.onKeyDown(keyCode, event);
}