Android Marshmallow应用程序中的位置权限问题 [英] Permission issues for location in android Marshmallow applicaton

查看:143
本文介绍了Android Marshmallow应用程序中的位置权限问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习开发一个Android应用程序,以便在Google开发人员论坛后获取设备位置:@ http://developer.android.com/training/location/retrieve-current.html#last-known
@ http://developer.android.com/training/location/receive-location-updates.html



该示例显示了如何使用Google PLay Services达到此目的。
但是 getLastLocation()函数总是返回null。


位置位置=
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

最初位置可能为空,我添加了以下内容以获得定期位置更新


LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest,this);


引发以下消息的异常


客户端必须拥有ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION
权限来执行任何定位操作


我已经在清单文件中添加了权限:


 < uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/> 
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>


我已打开Goog​​le地图以更新Google Play服务中的当前位置同样的问题仍然存在。
我是Android开发的新手,在发布这个问题之前,我已经搜索了很多,但没有找到任何解决方案为我工作。
我正在 Android Marshmallow 设备上进行测试,使用 Android Studio 1.3.2开发



AndroidManifest.xml

 <?xml version =1.0encoding = UTF-8\" >?; 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =newray.acpsa1>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>

< application
android:allowBackup =true
android:icon =@ mipmap / ic_launcher
android:label =@ string / app_name
android:theme =@ style / AppTheme>
< meta-data
android:name =com.google.android.gms.version
android:value =@ integer / google_play_services_version/>

< meta-data
android:name =com.google.android.maps.v2.API_KEY
android:value =@ string / google_maps_key/> ;

< activity
android:name =。MapsActivity
android:label =@ string / title_activity_maps>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>

< / application>

< / manifest>

Activity中的onCreate函数

  @Override 
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build() ;
mGoogleApiClient.connect();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);
/ *休息功能* /
}

onConnected功能

  @Override 
public void onConnected(Bundle bundle){
try {
Location location = LocationServices.FusedLocationApi .getLastLocation(mGoogleApiClient);
LatLng loc = new LatLng(21.356993,72.826647);
if(location == null){
Log.d(TAG,location is null);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
Log.d(标签,请求更新);
}
else {
loc = new LatLng(location.getLatitude(),location.getLongitude());
handleNewLocation(location);

} catch(Exception e){
Log.d(TAG,Error in onConnected。+ e.getMessage());


$ b

记录:

  XXXX:onConnected错误。客户必须具有ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限才能执行任何地点操作。 

build.gradle文件中的依赖关系:

 依赖项{
编译fileTree(dir:'libs',include:['* .jar'])
compile'c​​om.android.support:appcompat-v7 :23.1.0'
compile'c​​om.google.android.gms:play-services:8.1.0'
compile'c​​om.google.android.gms:play-services-location:8.1.0 '
}


解决方案

android SDK中的新东西。询问权限是每个开发人员都应该记住的最新更新之一。类似的事情发生在你身上。如果您的应用程序的目标版本为23,那么在开始使用它之前,您可能应该从用户处获得授权。只有当用户允许访问时,才能使用它。因此,开发人员需要在从用户获得应用许可的情况下处理所有测试用例。例如,您的应用程序需要访问该位置。在早期版本的安装时,它显示了权限对话框,并且他们被授予了清单文件中定义的权限给应用程序。这件事变成了棉花糖。现在每个应用程序都必须获得用户的许可才能访问它。



查看开发人员网站并了解如何引导用户进入请求运行时权限的应用程序的基本步骤进入应用程序。



有很多网站向您展示了关于如何向Marshmallow提出许可的基本教程。很少有人会建议如下



使用运行时权限构建更好的应用程序



权限设计指南



此链接指导您了解如何在请求权限之前引导用户进入应用程序的基本信息。



谢谢。

I am learning to develop an android application for getting device location following Google developers forum: @http://developer.android.com/training/location/retrieve-current.html#last-known @http://developer.android.com/training/location/receive-location-updates.html

The example shows how to use Google PLay Services for this purpose. But the getLastLocation() function always return null.

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

As initially the location might be null, I added the following lines to receive periodic location updates

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

which throws an Exception with the following Message

Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations

I have already added the permissions in manifest file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I have opened Google Maps to update current location in Google Play Service as well, but the same problem persists. I am newbie to Android development, and before posting this question I already searched up a lot but didn't find any solution working for me. I am testing it on Android Marshmallow device, developing using Android Studio 1.3.2

AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

onCreate function in Activity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(10 * 1000)        
                .setFastestInterval(1 * 1000); 
        /* Rest functionality */
    }

onConnected function

@Override
    public void onConnected(Bundle bundle) {
        try{
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        LatLng loc = new LatLng(21.356993, 72.826647);
        if (location == null) {
            Log.d(TAG,"location is again null");
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
            Log.d(TAG, "Requested for updates");
        }
        else {
            loc = new LatLng(location.getLatitude(),location.getLongitude());
            handleNewLocation(location);
        }            
        } catch (Exception e) {
            Log.d(TAG, "Error in onConnected.  "+e.getMessage());

        }
    }

Logs:

XXXX: Error in onConnected.  Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.

dependencies in build.gradle file:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.google.android.gms:play-services:8.1.0'
    compile 'com.google.android.gms:play-services-location:8.1.0'
}

解决方案

In Marshmallow there are lots of new things in android SDK. Asking the permission is one of it's new update that every developer should keep in mind. The similar thing happen with you. if your application target version 23 then you probably should have to take grant from the user before start to use it. You can only use it if user allow to access.Therefor developer need to take care all the test-case while taking the permission from the user into the application

For example your application need the access of the Location. In earlier version of android at time on installed it was show the permission dialog and they were granted the defined permission in your manifest file to the application. This things changed into the Marshmallow. Now each application should have to take permission from user to access the it.

See the developer sites and learn the basic steps for how to guide the user into the application for asking the runtime permission into the application.

There are lots of sites showing your the basic tutorial about how to ask the permission in Marshmallow. few can suggest as below

Building better application with runtime permission

Permissions design guide

This link guide you the basic info of how to guide the user into the application before asking the permission.

Thanks.

这篇关于Android Marshmallow应用程序中的位置权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆