Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

android - ContactsContract.CommonDataKinds.Phone.getTypeLabel返回摩托罗拉设备上的奇怪字符串(ContactsContract.CommonDataKinds.Phone.getTypeLabel returns weird strings on Motorola devices)

I'm using ContactsContract.CommonDataKinds.Phone.getTypeLabel to get the String of the phone type of the user (1 --> home, 2 --> mobile, 3 --> work, etc.).

(我正在使用ContactsContract.CommonDataKinds.Phone.getTypeLabel来获取用户电话类型的字符串(1->家庭,2->手机,3->工作等)。)

Everything seem to work fine on most devices, but on Motorola devices the returned strings are some weird chars I can't even type here (looks like Korean/Japanese/Thai).

(在大多数设备上,一切似乎都可以正常工作,但是在摩托罗拉设备上,返回的字符串是一些我甚至不能在此处键入的奇怪字符(看起来像韩文/日文/泰文)。)

The locale of those devices is English and users also can't understand those weird strings.

(这些设备的语言环境为英语,用户也无法理解这些奇怪的字符串。)

My code is straight forward:

(我的代码很简单:)

return ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), type, "").toString();
  ask by user2634975 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Getting a pre defined label of a contact is pretty direct, however, if the user has set a custom label, then it's not direct, you can try the follwing to get the correct label :

(获取联系人的预定义标签是很直接的,但是,如果用户设置了自定义标签,则不是直接的,您可以尝试以下方法来获取正确的标签:)

int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            String label = context.getResources().getString(ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(type));
            if (label.equalsIgnoreCase("Custom")){
                label = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
            }

You can now directly use the label string.

(现在,您可以直接使用标签字符串。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...