选择联系人并获取电话号码并拨打电话

时间:2011-03-28 05:49:48

标签: android

点击联系人时我没有收到电话号码,我只收到一个联系人姓名 但没有数字。我被提到了一个相关的帖子,但仍然不满意 请指导我。

我的代码如下:

package com.Call_setup;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.widget.ListAdapter;
import android.widget.TextView;

public class Call_setup extends Activity {
    /** Called when the activity is first created. */
    Intent callIntent;
    private ListAdapter mAdapter;
    public TextView pbContact;
    public static String PBCONTACT;
    public static final int ACTIVITY_EDIT=1;
    private static final int ACTIVITY_CREATE=0;
    private static final int PICK_CONTACT = 0;
    private static final int PICK_CONTACT_REQUEST = 2;
      String id;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        call();
        //  onReceive(this,callIntent);
        IncomingCallReciever ic = new IncomingCallReciever();
        ic.onReceive(getApplicationContext(), callIntent);
    }
    private void call() {
        try {
          // Intent callIntent = new Intent(Intent.ACTION_CALL);
            /*Intent callIntent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
            callIntent.setData(Uri.parse("tel:123456"));
            startActivity(callIntent);
           // stopService(callIntent);
            */


            Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);
            }
            catch (ActivityNotFoundException activityException) {
           //  Log.e("helloandroid dialing example", "Call failed", e);
        }


    }
    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
      super.onActivityResult(reqCode, resultCode, data);
      ContentResolver cr = getContentResolver();
      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 name = c.getString(c.getColumnIndexOrThrow(People.Phones.NAME));
              String contactId = c.getString(c .getColumnIndex(ContactsContract.Contacts._ID));
              // Intent callIntent = new Intent(Intent.ACTION_CALL);

              System.out.println("number key is : " + name);
              System.out.println("reqCode is : " + reqCode);
              System.out.println("resultCode is : " + resultCode);
              System.out.println("contactId is :" + contactId);

             // String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
              /*callIntent.setData(Uri.parse("tel:"+number));
              startActivity(callIntent);*/

            }

          }
          break;
      }

   }
      }

    /*public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener=new MyPhoneStateListener();
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

        telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);*/

   /* public void onReceive( final Context context, final Intent intent ) 
    { 
      Log.i( "l", "onReceive()" ); 
      Log.i( "l", "context: " + context ); 
      Log.i( "l", "intent: " + intent ); 
      if( intent.getAction().equals( Intent.ACTION_NEW_OUTGOING_CALL ) ) 
      { 
        String number = intent.getExtras().getString( Intent.EXTRA_PHONE_NUMBER ); 
        Log.i( "l", "number: " + number ); 
      } 
    } */

1 个答案:

答案 0 :(得分:0)

Cursor phones = etContentResolver()
       .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));
}