与其他应用交互 Intent

来源:互联网 发布:工信部域名备案系统 编辑:程序博客网 时间:2024/05/20 05:56

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 "Share this photo with"String title = getResources().getString(R.string.chooser_title);// Create intent to show chooserIntent chooser = Intent.createChooser(intent, title);// Verify the intent will resolve to at least one activityif (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);}

接收

奖励:读取联系人数据
显示如何从“联系人”应用获取结果的代码不会详细说明如何实际从结果读取数据,但它需要对内容提供程序进行更深入的探讨。 但是,如果您很好奇,此处提供了更多的代码向您展示如何查询结果数据,从所选联系人获取电话号码:

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    // Check which request 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 selected contact            Uri contactUri = data.getData();            // We only need the NUMBER column, because there will be only one row in the result            String[] projection = {Phone.NUMBER};            // Perform the query on the contact to get the 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 be called from a separate thread to avoid blocking            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)            // Consider using CursorLoader to perform the query.            Cursor cursor = getContentResolver()                    .query(contactUri, projection, null, null, null);            cursor.moveToFirst();            // Retrieve the phone number from the NUMBER column            int column = cursor.getColumnIndex(Phone.NUMBER);            String number = cursor.getString(column);            // Do something with the phone number...        }    }}

4支持其他应用Intent请求

<activity android:name="ShareActivity">    <!-- filter for sending text; accepts SENDTO action with sms URI schemes -->    <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 text or 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>
原创粉丝点击