无法获取Android应用的位置权限 [英] Can't get location permission for Android app

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

问题描述

在我的清单中有以下内容:

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

我还在手机设置中设置了位置权限。



是的,GPS在手机上启用。



在这里,我检查权限:

  if(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_CODES.M){
int hasLocationPermission = mContext.checkSelfPermission(Manifest。 permission.ACCESS_FINE_LOCATION);
Log.d(TAG,location permission:+ hasLocationPermission);如果(hasLocationPermission!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(mContext,new String [] {Manifest.permission.ACCESS_FINE_LOCATION},1); // 0


}
hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
Log.d(TAG,location permission:+ hasLocationPermission); //仍然是0

$ / code>

如果我在我的清单和手机中为什么它仍然阻止我访问该位置?由于这个原因,它在经度和纬度上都返回0.0。

(Build.VERSION.SDK_INT> = 23){

$ b $>

 如果(checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),this)){
//你在这里获取位置

//使用
的代码}
else
{
requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),this);
}

}

同时声明在课堂上

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

if(ActivityCompat.shouldShowRequestPermissionRationale(_a,strPermission)){
Toast.makeText(getApplicationContext(),GPS权限允许我们访问位置数据,请在应用程序设置中允许其他功能。 .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;

$ b}

@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();
$ b $} else {

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

}
break;



您应该在声明


时给予许可

I have the following in my manifest:

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

I also have the location permission set in my phone's settings.

Yes, GPS is enabled on the phone.

Here, I check for the permission:

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            int hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            Log.d(TAG, "location permission: " + hasLocationPermission); // 0

            if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            }
            hasLocationPermission = mContext.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
            Log.d(TAG, "location permission: " + hasLocationPermission); // still 0
        }

If I have the permission in my manifest and in my phone's settings for the app, why is it still preventing me from accessing the location? It is returning 0.0 for both latitude and longitude because of this.

解决方案

Declare Where you use Oncreate Or Others

    if (Build.VERSION.SDK_INT >= 23){


    if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION,getApplicationContext(),this)) {
    //You fetch the Location here

    //code to use the 
    }
    else
    {
    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION,PERMISSION_REQUEST_CODE_LOCATION,getApplicationContext(),this);
    }

    }

Also Declare In class

    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;

    }
}

For other lower device you should give the permission as you declare

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

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