Unable to get phone number from device inspite of declaring required permissions in android manifest

放肆的年华 提交于 2019-12-10 12:22:39

问题


I am trying to get phone number from my device programmatically. I am trying with the following code:

TelephonyManager tMgr = (TelephonyManager) getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();

I have declared the below permissions in manifest file:

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

But still my application is crashing and the stack trace is as below:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: toadways.ways.toad.toadways, PID: 29620
java.lang.RuntimeException: Unable to start activity ComponentInfo{toadways.ways.toad.toadways/toadways.ways.toad.toadways.RegistrationScreen}: java.lang.SecurityException: getLine1NumberForDisplay: Neither user 10240 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 10240 nor current process has android.permission.READ_SMS.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getLine1NumberForDisplay(ITelephony.java:3717)
at android.telephony.TelephonyManager.getLine1NumberForSubscriber(TelephonyManager.java:2091)
at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:2069)
at toadways.ways.toad.toadways.RegistrationScreen.onCreate(RegistrationScreen.java:50)
at android.app.Activity.performCreate(Activity.java:6237)
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) 

Can anyone pleas elet me know what mistake am I doing and why am I getting this crash. All suggestions are welcome.


回答1:


Starting with API23 (Marshmallow), you need to request some permissions at runtime. It is described on http://developer.android.com/training/permissions/requesting.html

Basically:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
    Manifest.permission.READ_SMS);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_SMS},
            REQUEST_CODE_READ_SMS); // define this constant yourself
} else {
     // you have the permission
}

The activity referenced by thisActivity must implement ActivityCompat.OnRequestPermissionsResultCallback.html#onRequestPermissionsResult(int, java.lang.String[], int[]). In this method, if the permission was granted for your request, you can continue. The first int parameter is the request code, e.g. REQUEST_CODE_READ_SMS.

This is a basic example that doesn't take into account some other parameters, so read the doc to understand how it works.



来源:https://stackoverflow.com/questions/34018388/unable-to-get-phone-number-from-device-inspite-of-declaring-required-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!