无法使用 GPS 获取位置

Not able to fetch location using GPS

我正在尝试制作一个简单的应用程序来显示我的位置坐标。当我模拟代码时( nexus 6.0 ,它有效)。但是,当我通过 usb 调试(我的 htc 10)使用真实设备时,按下按钮显示我的坐标时什么也没有出现。知道为什么吗?这是我的代码:

    //Location
    textView = (TextView) findViewById(R.id.textView);
    button2 = (Button) findViewById(R.id.button2);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            textView.append("\n " + location.getLongitude() + " " + location.getLatitude());
            System.out.println(location.getLongitude());
            System.out.println(location.getLatitude());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };

    configure_button();

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    switch (requestCode) {
        case 10:
            configure_button();
            break;
        default:
            break;
    }
}

void configure_button() {
    // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
            PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
                    , 10);
        }
        return;
    }
    // this code won't execute IF permissions are not allowed, because in the line above there is return statement.
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            locationManager.requestLocationUpdates("gps", 5000, 0, listener);
        }
    });
}

我在清单中添加了这个:

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />

您应该在 oncreate 中声明 button2 点击侦听器,因为 configure_button() 方法中有 return 语句。

声明id后直接声明就可以了

  textView = (TextView) findViewById(R.id.textView);
    button2 = (Button) findViewById(R.id.button2);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            locationManager.requestLocationUpdates("gps", 5000, 0, listener);
        }
    });
   //rest of the code