我在Manifest.xml中添加了android.permission.ACCESS_COARSE_LOCATION
的权限,代码如下。
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation location = (GsmCellLocation) tm.getCellLocation();
int cellID = location.getCid();
int lac = location.getLac();
Toast.makeText(getApplicationContext(), cellID, Toast.LENGTH_LONG).show();
然而,它崩溃了下面的错误消息。请检查如何解决这类问题。
05-15 23:23:09.844 22602-22602/use.uevision.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: use.uevision.com.myapplication, PID: 22602
java.lang.RuntimeException: Unable to start activity ComponentInfo{cctvkim.truevision.com.myapplication/cctvkim.truevision.com.myapplication.MainActivity}: java.lang.SecurityException: getCellLocation: Neither user 10046 nor current process has android.permission.ACCESS_COARSE_LOCATION.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.SecurityException: getCellLocation: Neither user 10046 nor current process has android.permission.ACCESS_COARSE_LOCATION.strong text
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getCellLocation(ITelephony.java:3311)
at android.telephony.TelephonyManager.getCellLocation(TelephonyManager.java:963)
at cctvkim.truevision.com.myapplication.MainActivity.onCreate(MainActivity.java:21)
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
at android.app.ActivityThread.access$1100(ActivityThread.java:221)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7225)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
05-15 23:23:13.804 22602-22602/cctvkim.truevision.com.myapplication I/Process: Sending signal. PID: 22602 SIG: 9
答案 0 :(得分:0)
Yuo应该使用RuntimePermissions。使用Android M启动时,您需要在运行时询问权限。请参阅链接。
if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST);
}else{
//do your job
}
答案 1 :(得分:-1)
您需要添加READ_PHONE_STATE权限才能使用TelephonyManager。 在你的清单文件中添加此权限。