我正在使用Xamarin和Android Emulator。我想写一个简单的应用程序来抓取用户的电话号码。我正在使用
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)GetSystemService(TelephonyService);
var Number = mTelephonyMgr.Line1Number;
但它不起作用。该应用程序在第二行崩溃。我的问题是,
这是我得到的例外
+ ex {Java.Lang.SecurityException: Exception of type 'Java.Lang.SecurityException' was thrown.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in <filename unknown>:0
at Android.Runtime.JNIEnv.CallObjectMethod (IntPtr jobject, IntPtr jmethod) [0x00063] in /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:178
at Android.Telephony.TelephonyManager.get_Line1Number () [0x00043] in /Users/builder/data/lanes/1780/3518c4ce/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Telephony.TelephonyManager.cs:377
at UnwantedNumbers.MainActivity.<OnCreate>b__0 (System.Object param0, System.EventArgs param1) [0x00013] in e:\data\trunk\Yuan Miao\Coding Examples\Mobile App\UnwantedNumbers\UnwantedNumbers\MainActivity.cs:39
--- End of managed exception stack trace ---
java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10037 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getLine1Number(IPhoneSubInfo.java:231)
at android.telephony.TelephonyManager.getLine1Number(TelephonyManager.java:624)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
} Java.Lang.SecurityException
答案 0 :(得分:2)
它在你的异常android.permission.READ_PHONE_STATE
中说它。将该权限添加到您的清单中,您应该很高兴...