使用TelephonyManager获取位置会抛出java.lang.SecurityException

时间:2016-04-12 08:39:56

标签: android telephonymanager

您好,在我的应用中,我使用TelephonyManager来获取位置。这是一段代码:

        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);
        }
    }

在我的清单中:

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

这是错误:

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)

正如您所看到的,我已经在清单和权限检查中包含了所有必需的权限以及Android M的代码,我不知道为什么我仍然得到**java.lang.SecurityException**。有人请帮助我。

1 个答案:

答案 0 :(得分:1)

在致电tm.getCellLocation()之前,您必须等待权限检查流程完成。如:

if (ContextCompat.checkSelfPermission(getApplicationContext(),
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ){
    //do your request permission   
}else{
    // do your getLocation here
}

Activity - onRequestPermissionsResult中,您可以检查是否授予了权限,然后执行getLocation