【问题标题】:Can't access location无法访问位置
【发布时间】:2018-07-16 06:49:49
【问题描述】:

我正在尝试访问该位置并在单击按钮时在文本视图中显示纬度和经度。

这是我的代码:

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends Activity {

    TextView textView, textView1;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);
        textView1 = findViewById(R.id.textView2);
        button = findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    boolean enableOnly = true;
                    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                    List<String> providers = locationManager.getProviders(enableOnly);

                    for (String provider : providers) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                                Location location = locationManager.getLastKnownLocation(provider);
                                if (location != null) {
                                    textView.setText((int) location.getLatitude());
                                    textView1.setText((int) location.getLatitude());
                                } else {
                                    Toast.makeText(MainActivity.this, "sorry", Toast.LENGTH_SHORT).show();
                                }
                                return;
                            }
                        }
                    }
            }

            });

    }
}

我正在测试的 AVD 是 Android 8.1 我还在 AndroidManifest 中设置了用户权限以访问粗略和精细位置。位置权限也被授予应用程序。

但是每当我点击按钮时,什么都没有发生。 请帮帮我。

【问题讨论】:

  • 位置不为空,checkSelfPermission 不返回 false @BkSantiago
  • 尝试在 onLocationChanged(Location location) 方法下获取位置
  • 从 avd 设置中设置位置

标签: android android-location


【解决方案1】:
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == 
PackageManager.PERMISSION_GRANTED && 
checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == 
PackageManager.PERMISSION_GRANTED) {
    //do your task
}

此代码也仅适用于您提到的上述棉花糖设备

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

}

【讨论】:

    【解决方案2】:

    使用此代码:

    LocationManager mLocationManager;
    Location myLocation = getLastKnownLocation();
    
    private Location getLastKnownLocation() {
        mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
        List<String> providers = mLocationManager.getProviders(true);
        Location bestLocation = null;
        for (String provider : providers) {
            Location l = mLocationManager.getLastKnownLocation(provider);
            if (l == null) {
                continue;
            }
            if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                // Found best last known location: %s", l);
                bestLocation = l;
            }
        }
        return bestLocation;
    }
    

    【讨论】:

    • 我打赌测试它会在 70% 的时间里返回 null
    【解决方案3】:

    抱歉,我没有看到您已获得许可。 现在只要授予权限就使用 setUpGPS:

     LocationManager locationManager;
    
    void setUpGPS() {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    
            return;
        }
        locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                location.getLatitude();
                location.getLongitude();
            }
    
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
    
            }
    
            @Override
            public void onProviderEnabled(String s) {
    
            }
    
            @Override
            public void onProviderDisabled(String s) {
    
            }
        });
    
    }
    

    onLocationChanged 方法有两个方法:getLatitude 和 getLongitude。这些是用户的位置点。您可以将它们传递给双变量并使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多