安卓检索到无效的电话号码



我尝试了几乎所有其他答案或代码,但我的问题仍然存在。检索联系人的电话号码是任意的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

我在下面添加了检索设备电话号码和呼叫按钮所需的代码。 希望我能帮助您解决问题

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;
}
}

}

也添加这个在你的安卓清单

<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" />

最新更新