Android检索无效的电话号码

问题描述 投票:0回答:1

我几乎尝试了所有其他答案或代码,但问题仍然存在。获取的联系人电话号码是任意3-4位数字,而不是联系人的确切电话号码。当我继续拨打该号码时,它会拨到“ 6855”。请帮助。

代码

if(mWasButtonClicked){
        String[] projections = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
        String selectionClause = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
        String[] selectionArgs = new String[] {mContactID};


        Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projections,selectionClause,selectionArgs,null,null);

        try {
            if(c.getCount() == 0){return;}

            if(c!=null){
                while(c.moveToNext()){

                    c.moveToFirst();
                    phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    Log.d("TEST",phoneNumber);
                }
            }
        }finally {
            c.close();
        }
    }

用于通话按钮

mCallButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("tel:"+phoneNumber);
            startActivity(new Intent(Intent.ACTION_CALL,uri));
        }
    });

用于检索联系人ID

 Uri content_uri = data.getData();

        String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID};

        Cursor c = getContentResolver().query(content_uri,projection,null,null,null,null);

        try {
            if(c.getCount() == 0){return;}

            if(c!=null){
                while(c.moveToNext()){

                    c.moveToFirst();
                    String name = c.getString(0);
                    mGetNameButton.setText(name);
                    mContactID = c.getString(1);
                    Log.d("TEST",mContactID);
                }
            }
        }finally {
            c.close();
        }

和日志

2020-01-10 05:41:38.284 27904-27904/com.example.contactsuri D/TEST: 84
android android-cursor android-contentresolver
1个回答
0
投票

我添加了以下代码,以检索设备的电话号码和通话按钮。希望我能帮助您解决问题

private static final int PERMISSION_REQUEST_CODE = 1; //permission
String wantPermission = Manifest.permission.READ_PHONE_STATE; //permission
String getMyMobilePhoneNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //call this function
    getTheMobilePhoneNumber();

    //to able see the phone number
    TextView getPhone = findViewById(R.id.getPhone);
    getPhone.setText(getMyMobilePhoneNumber);

    //add permission
    if (!checkPermission(wantPermission)) {
        requestPermission(wantPermission);
    } else {
        Log.d("TAG", "Phone number: " + getTheMobilePhoneNumber());
        getMyMobilePhoneNumber = getTheMobilePhoneNumber();
    }


    //for calling
    Button btnCallNum = findViewById(R.id.btnCall);
    btnCallNum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentCall = new Intent(Intent.ACTION_DIAL);
            intentCall.setData(Uri.parse("tel:" + getMyMobilePhoneNumber));
            startActivity(intentCall);
        }
    });
}


//get your phone number by calling this
public String getTheMobilePhoneNumber() {
    TelephonyManager telMngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, wantPermission) != PackageManager.PERMISSION_GRANTED) {
        return null;
    }
    getMyMobilePhoneNumber = telMngr.getLine1Number();

    return getMyMobilePhoneNumber;
}

private void requestPermission(String permission) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
        Log.d("TAG NUM", "Phone state permission allows us to get phone number. Please allow it for additional functionality. ");
    }
    ActivityCompat.requestPermissions(this, new String[]{permission}, PERMISSION_REQUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("TAG", "Phone number: " + getTheMobilePhoneNumber());
            } else {
                Log.d("TAG NUM", "Permission Denied. We can't get phone number ");
            }
            break;
    }
}

private boolean checkPermission(String permission) {
    if (Build.VERSION.SDK_INT >= 23) {
        int result = ContextCompat.checkSelfPermission(this, permission);
        return result == PackageManager.PERMISSION_GRANTED;
    } else {
        return true;
    }
}

}

也将此添加到您的AndroidManifest中

 <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
© www.soinside.com 2019 - 2024. All rights reserved.