gpt4 book ai didi

android - ACCESS_FINE_LOCATION 在 list 中被授予,但总是被拒绝许可?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:30 27 4
gpt4 key购买 nike

我目前正在尝试获取用户当前位置的纬度和经度。我在 list 中启用了以下权限。 TargetSDK和min sdk都是23

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.name.app">


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false" />


<activity
android:name=".SplashScreen"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainMenu" />
<activity
android:name=".NeedToKnow" />
<activity
android:name=".SelectTopic" />
<activity
android:name=".DisplayTopicInformation"/>
<activity
android:name=".SelectCheckList" />
<activity
android:name=".ViewCheckList"/>
<activity
android:name=".FindLocation"></activity>
</application>

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


</manifest>

为此,我了解我需要对权限执行运行时检查。但是,当我执行以下操作时,我总是得到 -1 作为返回。 (PERMISSION_DENIED)

int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION);

尝试访问当前位置时是否遗漏了一个步骤?

完整代码在这里:

private String getZipcodeFromCurrentLocation() {

int permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_FINE_LOCATION);

String zip = null;
Location location = null;
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();

Log.i("updated lat", String.valueOf(latitude));
Log.i("updated lng", String.valueOf(longitude));
}


};

try {
if (PackageManager.PERMISSION_GRANTED == permissionCheck) {
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

} catch (SecurityException e){
e.printStackTrace();
}
return zip;

}

最佳答案

如果用户在手机设置中为您的应用禁用了位置权限,则您必须请求该权限。您将在下面找到有关如何执行此操作的示例代码。

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

public class MyActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

if(permissionGranted) {
// {Some Code}
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 200: {
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// {Some Code}
}
}
}
}
}

我还构建了一个类来处理上述所有内容并返回用户的位置。这是链接:https://www.dropbox.com/s/2mkyjok6mpna2yw/GPS.java?dl=0 .

关于android - ACCESS_FINE_LOCATION 在 list 中被授予,但总是被拒绝许可?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187588/

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