问题
Hi in my app I am using TelephonyManager to get location. Here is the piece of code:
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
doPermissionCheck();
location = (GsmCellLocation) tm.getCellLocation();//throws exception in this line
cellID = location.getCid();
lac = location.getLac();
doPermissionCheck():
private void doPermissionCheck() {
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{ Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION);
}
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MapsActivity.this,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
MY_PERMISSION_ACCESS_FINE_LOCATION);
}
}
In my manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
This is the error:
04-12 13:59:39.725 6841-6841/com.example.oik.offlinemap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.oik.offlinemap, PID: 6841
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.oik.offlinemap/com.example.oik.offlinemap.MapsActivity}: java.lang.SecurityException: getCellLocation: Neither user 10057 nor current process has android.permission.ACCESS_COARSE_LOCATION.
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: getCellLocation: Neither user 10057 nor current process has android.permission.ACCESS_COARSE_LOCATION.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.android.internal.telephony.ITelephony$Stub$Proxy.getCellLocation(ITelephony.java:2292)
at android.telephony.TelephonyManager.getCellLocation(TelephonyManager.java:826)
at com.example.oik.offlinemap.MapsActivity.getLocation(MapsActivity.java:79)
at com.example.oik.offlinemap.MapsActivity.onCreate(MapsActivity.java:45)
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)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
As you can see that I have included all the required permissions in manifest and permission check in code as well for android M, I don't know why I still get the **java.lang.SecurityException**
. Someone please help me.
回答1:
You must wait your permission checking process finished before calling tm.getCellLocation()
. Such as:
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ){
//do your request permission
}else{
// do your getLocation here
}
in your Activity
- onRequestPermissionsResult
, you can check is permission is granted or not then do getLocation
.
来源:https://stackoverflow.com/questions/36567884/using-telephonymanager-to-get-location-throws-java-lang-securityexception