在按钮上单击我想显示联系人列表并检索用户选择的联系人号码。这是我的代码:
public class MainActivity extends Activity {
int level;
final int PICK_CONTACT =1;
private TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = new TextView(this);
textView.setWidth(100);
textView.setHeight(100);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
@SuppressWarnings("deprecation")
//Contracts class is deprecated , please tell any other alternative if possible.
Intent intent= new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
//here am retrieving name , please tell how to retrieve number as well
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
textView.setText(name);
// TODO Fetch other Contact details as you want to use
}
}
break;
}
}
}
在清单文件中添加了权限:android.permission.READ_CONTACTS
不知道它实际上是否需要。
还请告诉我是否要检索用户从联系人列表中选择的多个联系人。
这是我的日志:
11-17 15:28:58.265: E/AndroidRuntime(12359): FATAL EXCEPTION: main
11-17 15:28:58.265: E/AndroidRuntime(12359): java.lang.IllegalStateException: Could not execute method of the activity
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View$1.onClick(View.java:2144)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View.performClick(View.java:2485)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View$PerformClick.run(View.java:9080)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Handler.handleCallback(Handler.java:587)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Looper.loop(Looper.java:130)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.app.ActivityThread.main(ActivityThread.java:3687)
11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invoke(Method.java:507)
11-17 15:28:58.265: E/AndroidRuntime(12359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-17 15:28:58.265: E/AndroidRuntime(12359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-17 15:28:58.265: E/AndroidRuntime(12359): at dalvik.system.NativeStart.main(Native Method)
11-17 15:28:58.265: E/AndroidRuntime(12359): Caused by: java.lang.reflect.InvocationTargetException
11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invoke(Method.java:507)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View$1.onClick(View.java:2139)
11-17 15:28:58.265: E/AndroidRuntime(12359): ... 11 more
11-17 15:28:58.265: E/AndroidRuntime(12359): Caused by: java.lang.IllegalArgumentException: URI: content://contacts, calling user: android.uid.system:1000, calling package is one of: [com.sec.android.app.popupuireceiver, com.android.Preconfig, com.sec.app.RilErrorNotifier, com.sec.android.providers.drm, com.android.server.vpn, com.android.systemui, com.android.providers.security, com.wssyncmldm, com.android.settings, com.wssnps, com.android.bluetoothtest, com.sec.android.app.servicemodeapp, com.sec.android.app.factorytest, com.android.settings.mt, com.samsung.InputEventApp, com.sec.dsm.system, com.sec.android.app.wlantest, com.samsung.AlertRecipients, com.sec.android.app.personalization, android, com.sec.android.app.selftestmode, com.google.android.backup, com.android.providers.settings, com.android.providers.subscribedfeeds, com.sec.android.app.lcdtest]
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Parcel.readException(Parcel.java:1326)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Parcel.readException(Parcel.java:1276)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.app.ActivityManagerProxy.getProviderMimeType(ActivityManagerNative.java:2846)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.content.ContentResolver.getType(ContentResolver.java:215)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.content.Intent.resolveType(Intent.java:3268)
11-17 15:28:58.265: E/AndroidRuntime(12359): at android.content.Intent.resolveTypeIfNeeded(Intent.java:3290)
11-17 15:28:58.265: E/AndroidRuntime(12359): at
11-17 15:28:58.265: E/AndroidRuntime(12359): at com.example.sms.MainActivity.onClick(MainActivity.java:82)