1、验证是否存在接收 Intent 的应用
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size()> 0;
如果 isIntentSafe 是 true,则至少有一个应用将响应该 Intent。如果它是 false,则没有任何应用可以处理该 Intent。
2、如果要执行的操作可由多个应用处理,并且用户可能 习惯于每次选择不同的应用 — 比如“共享”操作时,需要显示选择器,请使用 createChooser() 创建Intent 并将其传递给 startActivity()。
例如:
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Sharethis 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 atleast one activity
if(intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
这将显示一个对话框,其中包含响应传递给 createChooser() 方法的 Intent 的应用列表,并且将提供的文本用作对话框标题。
3、如何启动一个“允许用户选择联系人”的 Activity:
static final int PICK_CONTACT_REQUEST = 1; // The request code
...
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
4、接收选择的联系人数据
此处提供了代码向您展示如何查询结果数据,从所选联系人获取电话号码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check whichrequest it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Get the URI that points to the selectedcontact
Uri contactUri = data.getData();
// We only need the NUMBER column, becausethere will be only one row in the result
String[] projection = {Phone.NUMBER};
// Perform the query on the contact to getthe NUMBER column
// We don't need a selection or sort order(there's only one result for the given URI)
// CAUTION: The query() method should becalled from a separate thread to avoid blocking
// your app's UI thread. (For simplicity ofthe sample, this code doesn't do that.)
// Consider using CursorLoaderto perform the query.
Cursor cursor = getContentResolver()
.query(contactUri, projection, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from theNUMBER column
int column = cursor.getColumnIndex(Phone.NUMBER);
String number = cursor.getString(column);
// Do something with the phone number...
}
}
}
5、支持其他应用Intent请求
<activity android:name="ShareActivity">
<!-- filter for sending text; accepts SENDTO action with sms URIschemes -->
<intent-filter>
<action android:name="android.intent.action.SENDTO"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
<!-- filter for sending text or images; accepts SEND action and textor image data -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>