gpt4 book ai didi

android - 无法获取 Android 应用的位置权限

转载 作者:太空狗 更新时间:2023-10-29 14:47:39 28 4
gpt4 key购买 nike

我的 list 中有以下内容:

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

如果我在我的 list 和手机的应用程序设置中获得许可,为什么它仍然阻止我访问该位置?因此,纬度和经度都返回 0.0。

最佳答案

声明你在哪里使用 Oncreate 或其他

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

}

也在类里面声明

    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;

}
}

对于其他较低的设备,您应该在声明时给予许可

关于android - 无法获取 Android 应用的位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38142097/

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