安卓

我在使用模拟器时出现奇怪的错误。 我发现一个问题,9个月前有同样的问题,根本没有答案...

我使用的是谷歌播放服务的位置,以获得我的应用程序的位置,我敢肯定我的清单权限和一切工作完全在我的手机上,问题发生时只使用模拟器,我尝试了不同的模拟器在我的iMac和Windows机器。

我怀疑模拟器不支持这个基本功能!

这里是我的清单代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.orderme.ordermeandroid" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

这里是例外情况发生的地方:

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,mLocationRequest,this);

堆栈跟踪:

08-26 14:01:19.699  10157-10157/com.orderme.ordermeandroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.orderme.ordermeandroid, PID: 10157
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
        at android.os.Parcel.readException(Parcel.java:1599)
        at android.os.Parcel.readException(Parcel.java:1552)
        at com.google.android.gms.location.internal.zzg$zza$zza.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzi.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzj.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
        at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
        at com.google.android.gms.common.api.zzc$zza.zzb(Unknown Source)
        at com.google.android.gms.common.api.zzf.zza(Unknown Source)
        at com.google.android.gms.common.api.zzf.zzb(Unknown Source)
        at com.google.android.gms.common.api.zzi.zzb(Unknown Source)
        at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
        at com.orderme.ordermeandroid.Main.MainActivity.onConnected(MainActivity.java:184)
        at com.google.android.gms.common.internal.zzk.zzh(Unknown Source)
        at com.google.android.gms.common.api.zzg.zznt(Unknown Source)
        at com.google.android.gms.common.api.zzg.onConnected(Unknown Source)
        at com.google.android.gms.common.api.zzi$2.onConnected(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zzg.zzoD(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zza.zzs(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zzc.zzoF(Unknown Source)
        at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
        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 6.0+模拟器上运行这个,你有一个23或更高的targetSdkVersion

在这种情况下, ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION是Android 6.0运行时权限系统的一部分。 请修改您的应用以参与此系统,或者将您的targetSdkVersion降至23以下。


在Android 6.0(API级别23)中,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时授予应用程序权限

如何检查您是否有权限:

// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
        android.Manifest.permission.ACCESS_FINE_LOCATION);

如何请求您需要的权限:

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.ACCESS_FINE_LOCATION)
    != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
            MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);

    // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}

如何访问请求的结果:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
    case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
        // If request is cancelled, the result arrays are empty.
        if (grantResults.length > 0
            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

            // permission was granted, yay! Do the task you need to do.

        } else {

            // permission denied, boo! Disable the functionality that depends on this permission.
        }
        return;
    }
}
}

资料来源:http://developer.android.com/training/permissions/requesting.html

详细了解权限:https://guides.codepath.com/android/Understanding-App-Permissions


您无法从仿真器获取当前位置。 但是你可以模拟它来测试你的代码。 在这里你可以找到如何模仿。

编辑1:这里有一个注释

注意:如果您同时使用NETWORK_PROVIDER和GPS_PROVIDER,那么您只需要请求ACCESS_FINE_LOCATION权限,因为它包含两个提供者的权限。 (ACCESS_COARSE_LOCATION的权限仅包含NETWORK_PROVIDER的权限。)

您应该只使用ACCESS_FINE_LOCATION权限。 也许这有帮助。

链接地址: http://www.djcxy.com/p/90633.html

上一篇: android

下一篇: How to enable GPS in Eclipse Emulator for Android