我们如何获取android中任何设备的手机号码

时间:2018-09-14 04:48:28

标签: android telephonymanager

我正在使用此方法获取设备号。

TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String getSimNumber = telemamanger.getLine1Number();

但是此方法仅适用于android 7.0 ,不适用于6.0和8.0 请建议我如何在Android 6.0和8.0版中获取手机号码

1 个答案:

答案 0 :(得分:0)

 private void showContacts() {
    // Check the SDK version and whether the permission is already granted or not.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
        //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
    } else {
        String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
        Object object = getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"),
                main_data, "mimetype=?",
                new String[]{"vnd.android.cursor.item/phone_v2"},
                "is_primary DESC");
        String s1="";
        if (object != null) {
            do {
                if (!((Cursor) (object)).moveToNext())
                    break;
                // This is the phone Number
                s1 = ((Cursor) (object)).getString(4);
                Log.i("TestMOB",s1);
            } while (true);
            ((Cursor) (object)).close();
        }

    }
}

希望这会帮助您:)