简体   繁体   中英

getting phone number of a device in android

hey guys im trying to get phone number of my cell phone using android studio

i have entered permissions in the manifest file ie

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

and the code im using is

TelephonyManager tMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

String m=tMgr.getLine1Number();

but when i run my run my app it crashes. im unable to resolve this problem. kindly help me out. thanks!

the problem is with the statement

String m=tMgr.getLine1Number();

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.umk.stylefeedback, PID: 32333 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.umk.stylefeedback/com.example.umk.stylefeedback.NumberConfirmation}: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10097 nor current process has android.permission.READ_SMS. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10097 nor current process has android.permission.READ_SMS. at android.os.Parcel.readException(Parcel.java:1620) at android.os.Parcel.readException(Parcel.java:1573) at com.android.internal.telephony.ITelephony$Stub$Proxy.getLine1NumberForDisplay(ITelephony.java:3731) at android.telephony.TelephonyManager.getLine1NumberForSubscriber(TelephonyManager.java:2101) at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:2079) at com.example.umk.stylefeedback.NumberConfirmation.onCreate(NumberConfirmation.java:35) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Hello can share logcat.

There is no guaranteed solution to this problem because the phone number is not physically stored on all SIM-cards, or broadcasted from the network to the phone. This is especially true in some countries which requires physical address verification, with number assignment only happening afterwards. Phone number assignment happens on the network - and can be changed without changing the SIM card or device (eg this is how porting is supported).

I know it is pain, but most likely the best solution is just to ask the user to enter his/her phone number once and store it.

Umair,

You can copy all phonebook contact and insert own app db

 public void fetchContacts() {
        ////Log.e("step ", "1");
        String phoneNumber, phonetype = null;

        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
        Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

        // Loop for every contact in the phone
        if (cursor.getCount() > 0) {

            while (cursor.moveToNext()) {
                String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
                String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));

                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?"
                            , new String[]{contact_id}, DISPLAY_NAME);
                    while (phoneCursor.moveToNext()) {
                        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                        String string = phoneNumber.replaceAll("[^\\d]", "");
                        // String where2 = childJSONObject.getString("phone_number");
                        if (string.length() > 10)
                            string = string.substring(string.length() - 10);
                        JSONObject object = new JSONObject();
                        if (ValidationUtil.isValidMobileNumber(string)) {
                            if (string.length() > 2) {
                                try {
                                    object.put("name", helper.ConvertCamel(name));
                                    object.put("phone", Long.parseLong(string.trim()));
                                    objects.add(object);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                    phoneCursor.close();
                }
            }
            cursor.close();
        }
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM