我正在使用此功能获取Android手机号码,但没有成功,有人能为我提供此任务的解决方案吗?
我的功能:
public static String getPhoneDevice(Context mAppContext ){
String strPhone = "";
// metodo 1
try{
TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
strPhone = tMgr.getLine1Number();
}
catch (Exception e) {
}
// metodo 2
if (strPhone.isEmpty()) {
try{
String main_data[] = {"data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"};
Object object = mAppContext.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");
if (object != null) {
do {
if (!((Cursor) (object)).moveToNext())
break;
strPhone = ((Cursor) (object)).getString(4);
} while (true);
((Cursor) (object)).close();
}
}
catch (Exception e) {
}
}
return strPhone;
}
明确许可:
<!-- para acceso al numero telefono -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />