如何访问联系人在我的Andr​​oid程序我的、联系人、程序、Andr

2023-09-03 21:36:43 作者:╭懶懶de微笑

我在做一个文本消息应用程序,并要访问联系人在我的Andr​​oid应用程序

I'm making an text message application and want to access contacts in my Android application .

我要访问联系人,如果他们实际的联系人列表中的位置。当选择了我需要回到我的活动中,我可以发短信给那个人。或者是有可能获得其中的联系人存储在数据库?

I want to access contacts as if they where in the actual contact list. When selected I need to return to my activity in which I can send an SMS to that person. Or is it possible to access the database in which contacts are stored?

我的code如下所示:

My code is shown below:

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
String no = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

我的LogCat中看起来是这样的:

My LogCat looks like this:

09-07 12:46:15.458: DEBUG/AndroidRuntime(473): Shutting down VM
09-07 12:46:15.458: WARN/dalvikvm(473): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): FATAL EXCEPTION: main
09-07 12:46:15.478: ERROR/AndroidRuntime(473): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-512D3F3D533B/1 flg=0x1 (has extras) }} to activity {task.list/task.list.Msgactivity}: java.lang.IllegalArgumentException: column 'data1' does not exist
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.os.Looper.loop(Looper.java:123)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at java.lang.reflect.Method.invoke(Method.java:507)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at dalvik.system.NativeStart.main(Native Method)
09-07 12:46:15.478: ERROR/AndroidRuntime(473): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at task.list.Msgactivity.onActivityResult(Msgactivity.java:99)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
09-07 12:46:15.478: ERROR/AndroidRuntime(473):     ... 11 more
09-07 12:46:15.489: WARN/ActivityManager(61):   Force finishing activity task.list/.Msgactivity

谁能帮我?

推荐答案

使用code挑联系人:

use this code to pick contact:

 Button button = (Button)findViewById(R.id.pickcontact);

    button.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                 Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.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())
          {
          String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

          String hasPhone =
          c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

          if (hasPhone.equalsIgnoreCase("1")) 
          {
         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
            phones.moveToFirst();
            String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           // Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show();
            setCn(cNumber);
          }
         }
       }
    }
   }