0

我正在开发 QuickContactBadge。我想要做的是在 QuickContactBadge 中显示我的应用程序图标,如 facebook 或 gmail,当用户按下图标时,它将启动我的应用程序一个选定的活动。当活动启动时,我想获取用户选择的电话号码。

目前我的应用程序正在显示 QuickContactBadge 并且徽章也启动了应用程序中的主要活动,但我无法获得用户启动应用程序的电话号码表单。

我的代码如下:

@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);
    }

它获取 Intent.getData 但在 hasPhone 号码上出现异常,因为列不存在并且如果我在获取电话号码无法获取它时忽略此异常,也会在代码中下降。请帮助我在哪里做错了。

我已使用此代码在 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>
4

1 回答 1

1

我没有仔细查看您的代码,但这对我有用:

        // 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;
    }
}
于 2012-12-17T08:40:11.403 回答