gpt4 book ai didi

android - android Marshmallow 应用程序中位置的权限问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:40 25 4
gpt4 key购买 nike

我正在学习开发一个 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 服务。但是 getLastLocation() 函数总是返回 null。

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

由于最初位置可能为空,我添加了以下行以接收定期位置更新

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

它会抛出异常并显示以下消息

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

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

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

我也打开了 Google map 来更新 Google Play 服务中的当前位置,但同样的问题仍然存在。我是 Android 开发的新手,在发布这个问题之前我已经搜索了很多但没有找到适合我的解决方案。我正在 Android Marshmallow 设备上测试它,使用 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>

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);
/* Rest functionality */
}

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

}
}

日志:

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

build.gradle 文件中的依赖:

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'
}

最佳答案

您问题的直接解决方案/示例:

package com.my.packagename;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private static final int PERMISSION_REQUEST_CODE_LOCATION = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),CopyOfMap.this)) {
fetchLocationData();
}
else
{
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),CopyOfMap.this);
}
}

public static void requestPermission(String strPermission,int perCode,Context _c,Activity _a){

if (ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
Toast.makeText(getApplicationContext(),"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {

ActivityCompat.requestPermissions(_a,new String[]{strPermission},perCode);
}
}

public static boolean checkPermission(String strPermission,Context _c,Activity _a){
int result = ContextCompat.checkSelfPermission(_c, strPermission);
if (result == PackageManager.PERMISSION_GRANTED){

return true;

} else {

return false;

}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {

case PERMISSION_REQUEST_CODE_LOCATION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

fetchLocationData();

} else {

Toast.makeText(getApplicationContext(),"Permission Denied, You cannot access location data.",Toast.LENGTH_LONG).show();

}
break;

}
}


private void fetchLocationData()
{
//code to use the granted permission (location)
}
}

在这里,我将 requestPermission() 和 checkPermission() 方法设置为 public static,因为我可以将它们重新用于其他权限请求,例如电话调用、写入存储等。希望这会对某些人有所帮助。 :-)

关于android - android Marshmallow 应用程序中位置的权限问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com