如何获得联系方式

如何获得联系方式

本文介绍了如何获得联系方式(即编号名称等)的一次活动是由发射QuickContactBadge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作QuickContactBadge。我想要做的就是展示像Facebook或Gmail在QuickContactBadge我的应用程序图标,在图标的用户preSS将推出我的应用程序选定的活动。而当活动推出的是我想要得到哪些用户选择的电话号码。

目前我的应用程序是显示QuickContactBadge和徽章也推出了应用程序的主要活动,但我没能获得哪些用户启动应用程序的电话号码格式。

我的code是如下:

  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(getIntent()的getData()!= NULL){
            光标光标= getContentResolver()查询(getIntent()的getData(),NULL,NULL,NULL,NULL);
            如果(指针!= NULL){
                而(cursor.moveToNext()){
                    字符串的ContactID = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts._ID));
                    字符串hasPhone = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    如果(的Integer.parseInt(hasPhone)== 1){
                        //你知道有多少所以现在查询像这样
                        光标手机= getContentResolver()查询(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                空值,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=? ,
                                新的String [] {}的ContactID,NULL);
                        而(phones.moveToNext()){
                            字符串phoneNumber的= phones.getString(
                                    phones.getColumnIndex(
                                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        phones.close();
                    }其他{
                        Toast.makeText(这一点,无效的联系,Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
        的setContentView(R.layout.main);
    }

据混得hasPhone号Intent.getData但异常,因为列不存在,并在code也下来了,如果我忽略这个例外在获取电话号码无法得到它。 PLZ帮我哪里做错了。

我用这个code,以显示我的QuickContactBadge应用

 <活动
    机器人:名字=。SandBoxProjectActivity
    机器人:标签=@字符串/ APP_NAME>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.MAIN/>        <类机器人:名字=android.intent.category.LAUNCHER/>
    &所述; /意图滤光器>
    &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.VIEW/>        <类机器人:名字=android.intent.category.DEFAULT/>        <数据机器人:mime类型=vnd.android.cursor.item /名/>
    &所述; /意图滤光器>
< /活性GT;


解决方案

我没仔细看你的code,但这个工作对我来说:

  //检查活动从发起接触
    如果(getIntent()的getData()!= NULL){
        光标光标= getContentResolver()查询(getIntent()的getData(),NULL,NULL,NULL,NULL);
        如果(光标=空&放大器;!&放大器; cursor.moveToNext()){
            字符串的ContactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
            cursor.close();            selectContactNumber(的ContactID);
        }
    }
}私人无效selectContactNumber(字符串的ContactID){
    ArrayList的<串GT; numbersArr =新的ArrayList<串GT;();    //你知道它有许多所以现在查询像这样
    。光标手机= getContentResolver()查询(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,空,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=+的ContactID,NULL,NULL);
    而(phones.moveToNext()){
        串phoneNumber的= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        numbersArr.add(phoneNumber的);
    }
    phones.close();    开关(numbersArr.size()){
    情况下0:
        mNumberSelectorDialog = DialogFactory.getInstance()createNoNumbersMessageDialog(本)。
        mNumberSelectorDialog.show();
        打破;
    情况1:
        setNumberToCall(numbersArr.get(0));
        打破;
    默认:
        mNumberSelectorDialog = DialogFactory.getInstance()createNumberSelectorDialog(这一点,numbersArr.toArray(新的String [0]))。
        mNumberSelectorDialog.show();
        打破;
    }
}

I am working on QuickContactBadge. What I want to do is to show my app icon in the QuickContactBadge like facebook or gmail, and when the user press on the icon it will launch my application a selected activity. And when the the activity is launch i want to get the phone number which user selected.

Currently my application is showing QuickContactBadge and the badge also launch the main activity in the application but i am not able to get the phone number form which user launch the application.

My code is as follow:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getIntent().getData() != null) {
            Cursor cursor = getContentResolver().query(getIntent().getData(), null, null, null, null);
            if(cursor != null) {
                while (cursor.moveToNext()) {
                    String contactId = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts._ID));
                    String hasPhone = cursor.getString(cursor.getColumnIndex(
                            ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    if (Integer.parseInt(hasPhone) == 1) {
                        // You know have the number so now query it like this
                        Cursor phones = getContentResolver().query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?" ,
                                new String[]{contactId}, null);
                        while (phones.moveToNext()) {
                            String phoneNumber = phones.getString(
                                    phones.getColumnIndex(
                                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        phones.close();
                    } else{
                        Toast.makeText(this, "Invalid Contact", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
        setContentView(R.layout.main);
    }

It getting the Intent.getData but exception on hasPhone number because column does not exist and also down in the code if I ignore this exception while getting the phone number unable to get it. Plz help me where i am doing wrong.

I have use this code to display my app in the QuickContactBadge

<activity
    android:name=".SandBoxProjectActivity"
    android:label="@string/app_name" >
    <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" />

        <category android:name="android.intent.category.DEFAULT" />

        <data android:mimeType="vnd.android.cursor.item/name" />
    </intent-filter>
</activity>
解决方案

I didn't look closely on your code, but this worked for me:

        // check if activity was launched from contacts
    if (getIntent().getData() != null) {
        Cursor cursor = getContentResolver().query(getIntent().getData(), null, null, null, null);
        if (cursor!=null && cursor.moveToNext()) {
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
            cursor.close();

            selectContactNumber(contactId);
        }
    }


}

private void selectContactNumber(String contactId) {
    ArrayList<String> numbersArr = new ArrayList<String>();

    // You know it has a number so now query it like this
    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
    while (phones.moveToNext()) {
        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        numbersArr.add(phoneNumber);
    }
    phones.close();

    switch (numbersArr.size()) {
    case 0:
        mNumberSelectorDialog = DialogFactory.getInstance().createNoNumbersMessageDialog(this);
        mNumberSelectorDialog.show();
        break;
    case 1:
        setNumberToCall(numbersArr.get(0));
        break;
    default:
        mNumberSelectorDialog = DialogFactory.getInstance().createNumberSelectorDialog(this, numbersArr.toArray(new String[0]));
        mNumberSelectorDialog.show();
        break;
    }
}

这篇关于如何获得联系方式(即编号名称等)的一次活动是由发射QuickContactBadge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 00:33