正在尝试检索 Android 设备上的位置...适用于模拟器但不适用于真实设备

Attempting to retrieve location on Android Device... works on emulator but not real device

我的问题:我正在尝试创建一个可以在全校范围内使用的应用程序。它需要不断访问位置。我按照在线教程完成了下面的代码。此代码非常适用于我的 android 模拟器,但不适用于我的设备。我在我的设备上收到了权限请求,但没有任何更新。现在,奇怪的是,如果我在我的设备上使用模拟位置应用程序,这个应用程序就可以工作。但仅限于模拟位置应用程序。

有人对如何解决这个问题有任何建议吗? (xml 文件只是一个文本视图。就像我说的,我只是先测试一下)

package com.example.dylan.locationexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private LocationManager locationManager;
    private LocationListener locationListener;

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

        locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d("Location: ", location.toString());

                TextView tv = findViewById(R.id.textView);
                tv.setText(tv.getText() + Double.toString(location.getLatitude()) + "\t" + Double.toString(location.getLongitude()) + "\n");

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };



        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);

        }else{

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);


            }
        }
    }


}

请确保清单中包含以下内容

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

并在您的物理设备上启用 GPS。

如果问题仍然存在,请检查手机 phone 上应用程序的权限设置。您可能拒绝了访问位置的权限。

这是我正在使用的代码:

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    com.google.android.gms.location.LocationListener mLocationListener = new com.google.android.gms.location.LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            listener.onLocation(location);
        }
    };
    if(location != null) {
        listener.onLocation(location);
    }else{
        locationListener = new android.location.LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                listener.onLocation(location);
                locationManager.removeUpdates(locationListener);
                Log.d(TAG,"Location: " + String.valueOf(location.getLongitude()));
            }
            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }
            @Override
            public void onProviderEnabled(String s) {

            }
            @Override
            public void onProviderDisabled(String s) {

            }
        };
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }

我实际上遇到了同样的事情,我找到了一个解决方案来让它在物理设备上运行。 禁用 WI-FI 连接即可。不知道它是如何或为什么起作用的。现在我刚刚重新打开 WI-Fi 连接,它又能正常工作了。如果 Wi-fi 对您不起作用,请尝试重新启动设备

您可能还需要稍等片刻,它才能在房子周围移动时开始获取位置,因此它会不断变化。