java Android 2.1 如何获取联系人的电话号码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2901187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android 2.1 How to get Phone Numbers of contacts
提问by Brandon Delany
I am new to Android and have been working on an app that needs to get all of the user's contact's phone numbers. Apparently the code I have does not work with the 2.1 SDK. So far here is the code I am using:
我是 Android 新手,一直在开发一个需要获取用户所有联系人电话号码的应用程序。显然,我拥有的代码不适用于 2.1 SDK。到目前为止,这是我正在使用的代码:
String[] projection = new String[] { Phone.NUMBER };
Cursor c = managedQuery( Phone.CONTENT_URI, projection, null, null, null );
int colIndex = -1;
try {
colIndex = c.getColumnIndexOrThrow( Phone.NUMBER );
} catch( Exception e ) {
print( e.getMessage() );
}
print( "Column Index = " + colIndex );
//count is equal to 3
for( int i = 0; i < count; i++ ){
try {
print( c.getString( 2 ) ); //the 2 used to be colIndex
} catch ( Exception e ) {
print( e.getMessage() );
}
}
It seems that no matter what I pass into c.getString() it keeps telling me that I passed in -1. But I even hardcoded the 2, and it says the same thing. Any help would be much appreciated.
似乎无论我传入 c.getString() 什么,它一直告诉我我传入了 -1。但我什至对 2 进行了硬编码,它说的是同样的事情。任何帮助将非常感激。
回答by Karan
Please check the following link on how to use the Android 2.0 Contacts API.
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
请查看以下链接,了解如何使用 Android 2.0 Contacts API。
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/
HTH !
哈!

