6

我开发了一个简单的 Android 应用程序,在这个简单的应用程序中,我想获取 GPS 纬度和经度数据,它在我的模拟器中正常工作,但是当我安装在真正的 Android 设备上时它不起作用。请帮我解决这个问题,以下是我的代码。提前致谢

public class getLocation extends AppCompatActivity {

private Button btnGetLocation;
private TextView textGPS;
private LocationManager locationManager;
private LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_location);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    btnGetLocation = (Button) findViewById(R.id.btnGetLocation);
    textGPS = (TextView) findViewById(R.id.textViewGps);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textGPS.setText("lat: "+location.getLatitude()+" long: "+location.getLongitude());

        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {
            startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

        }
    };

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.INTERNET
            },10);
            return;
        }
    }else{
        configureButton();
    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    switch (requestCode){
        case 10:
            if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                configureButton();
            return;
    }
}

private void configureButton() {

    btnGetLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            locationManager.requestLocationUpdates("gps", 0, 0, locationListener);
        }
    });
}

}

模拟器截图: 在此处输入图像描述

手机截屏: 在此处输入图像描述

4

3 回答 3

5

您只请求 GPS...您的设备需要与卫星联系!在太空!走到外面去测试一下!这是测试仅使用 GPS 的代码时的常见问题,它在室内效果不佳。

此外,你不应该使用硬编码的“gps”,你应该使用:

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

至于使用 FusedLocationProviderAPI,您会损失一些准确性,但它确实提供了很大改进的电池性能,因为它严重依赖网络位置,并且仅在绝对必要时才使用 GPS。有关如何使用它的完整示例,请参见此处: https ://stackoverflow.com/a/30255219/4409409

于 2016-04-09T07:40:17.090 回答
2

将此添加到您的代码中。当这个应用程序在室内运行时返回最后一个已知位置。

        // Get the location from the given provider
        Location location = locationManager
                .getLastKnownLocation(provider);

        locationManager.requestLocationUpdates(provider, 1000, 1, this);
于 2016-04-09T08:32:18.240 回答
1

Google Play 服务位置 API比Android 框架位置 API (android.location) 更适合作为向您的应用添加位置感知的一种方式。

您可以在此处找到如何使用它的示例

PS不要忘记检查您的设备是否授予权限。

于 2016-04-09T06:56:02.947 回答