0

我正在尝试以编程方式从我的设备中获取电话号码。我正在尝试使用以下代码:

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

我在清单文件中声明了以下权限:

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

但我的应用程序仍然崩溃,堆栈跟踪如下:

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) 

任何人都可以请让我知道我在做什么错误以及为什么我会崩溃。欢迎所有建议。

4

1 回答 1

6

从 API23 (Marshmallow) 开始,您需要在运行时请求一些权限。它在http://developer.android.com/training/permissions/requesting.html上有描述

基本上:

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

引用的活动thisActivity必须实现ActivityCompat.OnRequestPermissionsResultCallback.html#onRequestPermissionsResult(int, java.lang.String[], int[])。在这种方法中,如果您的请求获得了许可,您可以继续。第一个int参数是请求代码,例如REQUEST_CODE_READ_SMS

这是一个基本示例,没有考虑其他一些参数,因此请阅读文档以了解其工作原理。

于 2015-12-01T11:09:32.493 回答