如何使用主要联系人号码启动拨号器?

发布于 2024-10-13 12:34:59 字数 300 浏览 4 评论 0原文

任何人都可以帮助我实现以下目标,

给定在之前的活动期间通过“ACTION_PICK”获得的联系人的 Uri,我想启动 ACTION_DIAL 意图来呼叫该联系人。

但不确定如何解决这个问题,我是否需要首先查询以查找与联系人关联的主要号码,或者是否有更简单的方法?我希望我可以使用联系人的 Uri 启动 ACTION_DIAL,它会自己找出要做什么,但这不起作用。

我们一如既往地感谢您的建议。

PS 此代码适用于 Froyo 及以上版本,因此使用 ContactsContract 而不是旧版本。

谢谢,

Can anyone help me achieve the following,

Given a Uri to a contact already obtained via an 'ACTION_PICK' during a previous activity, I want to launch an ACTION_DIAL intent to call the contact.

Not sure how to go about this though, do I first need to query to find the primary number associated with the contact or is there a simpler approach? I was hoping I could just launch ACTION_DIAL with the Uri of the contact and it would figure out what to do itself but that doesn't work.

Your advice as always is appreciated.

PS This code is for Froyo onwards, so using ContactsContract not the older stuff.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-10-20 12:34:59

为了其他人的利益,使用从 ACTION_PICK 意图获取的联系人的 URI 启动 ACTION_DIAL/ACTION_CALL 意图是行不通的。这是一个无效参数。

因此,我必须通过几个查询来确定联系人的电话号码。我也只寻找“主”电话。

我执行该任务的代码如下,也许我不需要执行初始查询来获取 ID,因为我已经有了 URI,但我还没有找到替代方案,所以如果有人可以提出其他建议,请让我知道:-),

// passing in String uri - obtained via contacts based ACTION_PICK 
        String contactId = null;
        Cursor contactCursor =  callingActivity.managedQuery(Uri.parse(uri), null, null, null, null); 
        if (contactCursor.moveToFirst())
        {
            contactId =
                contactCursor.getString(contactCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
        } 

        Cursor c = callingActivity.managedQuery(Data.CONTENT_URI,
          new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
          Data.CONTACT_ID + "=?" + " AND "
                  + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE
                  + "' AND " + Phone.IS_PRIMARY + " != 0",
          new String[] {contactId}, null);


        if (c.moveToNext()) {
            result = c.getString(c.getColumnIndexOrThrow(Phone.NUMBER));
            if (DEBUG) Log.d(TAG, "GOT PHONE : "+c.getString(c.getColumnIndexOrThrow(Phone.NUMBER)) + ","
                    +c.getString(c.getColumnIndexOrThrow(Phone.LABEL)) + ","
                    +c.getString(c.getColumnIndexOrThrow(Phone.TYPE)));
        }

www.dbws.net

Well for the benefit of others, launching ACTION_DIAL/ACTION_CALL intent with the URI for a contact obtained from an ACTION_PICK intent does not work. It's an invalid parameter.

So I had to determine the phone number for the contact via a couple of queries. I also only look for the 'Primary' telephone.

My code to do the task is as follows, Perhaps I don't need to do the initial query to obtain the ID as I have the URI already but I haven't figured out an alternative yet so if anyone can suggest otherwise please let me know :-) ,

// passing in String uri - obtained via contacts based ACTION_PICK 
        String contactId = null;
        Cursor contactCursor =  callingActivity.managedQuery(Uri.parse(uri), null, null, null, null); 
        if (contactCursor.moveToFirst())
        {
            contactId =
                contactCursor.getString(contactCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
        } 

        Cursor c = callingActivity.managedQuery(Data.CONTENT_URI,
          new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
          Data.CONTACT_ID + "=?" + " AND "
                  + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE
                  + "' AND " + Phone.IS_PRIMARY + " != 0",
          new String[] {contactId}, null);


        if (c.moveToNext()) {
            result = c.getString(c.getColumnIndexOrThrow(Phone.NUMBER));
            if (DEBUG) Log.d(TAG, "GOT PHONE : "+c.getString(c.getColumnIndexOrThrow(Phone.NUMBER)) + ","
                    +c.getString(c.getColumnIndexOrThrow(Phone.LABEL)) + ","
                    +c.getString(c.getColumnIndexOrThrow(Phone.TYPE)));
        }

www.dbws.net

江湖彼岸 2024-10-20 12:34:59

它会帮助你,

Intent myintent = new Intent(Intent.ACTION_CALL); 
            Log.v("phno",""+phNo);
           myintent.setData(Uri.parse("tel:" +phNo ));  
           myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           startActivity(myintent);

it will help you,

Intent myintent = new Intent(Intent.ACTION_CALL); 
            Log.v("phno",""+phNo);
           myintent.setData(Uri.parse("tel:" +phNo ));  
           myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
           startActivity(myintent);
复古式 2024-10-20 12:34:59

添加

<uses-permission android:name="android.permission.CALL_PHONE" />

不要忘记在清单文件中

Don't forget to add

<uses-permission android:name="android.permission.CALL_PHONE" />

in Manifest file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文