隐式 Intent 的一般使用

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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洛克Lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值