Android Studio - 获取 GPS 数据

Android Studio - get GPS data

我正在尝试从我的 android phone 获取 GPS 数据,在这段代码中,我只是想获取纬度。
我没有使用onLocationChanged这个方法,因为我只想获取一次数据。
我没有收到任何错误或警告。

package com.example.gpstryxyz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LocationListener {

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

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 10, this);
        }

        Location loc1 = new Location("gps");
        double value1 = loc1.getLatitude();

        TextView tv = (TextView) findViewById(R.id.gpsTextView);
        tv.setText(String.valueOf(value1));
    }

    @Override
    public void onLocationChanged(Location location) {

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

所以我在 phone 上构建应用程序,打开 GPS,走出去,启动应用程序,输出为:

0.0

使用 onLocationChangeFunction

@Override
public void onLocationChanged(Location location) {
    LatLng CurrentLatLng = new LatLng(location.getLatitude(), location.getLongitude());
}

我想回答我自己的问题。
我没有在上面的代码中使用 requestPermissions() 方法,甚至在 if 语句中使用了错误类型的权限。
我也不知道方法 requestLocationUpdates() 和回调方法 onRequestPermissionsResult() 是如何工作的。 (ctrl + Q 帮助了这里)

package com.example.gpstryxyz;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements LocationListener {


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

        //app seemed to work only with gps, but does not update during runtime, quit and restart app to get new location
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            LocationManager locationManager;
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            //registration (if this call succeeds, it calls the onLocationChanged() method)
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, this);
        } else {
            TextView tv = (TextView) findViewById(R.id.gpsTextView);
            tv.setText(R.string.noPermission);
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) throws SecurityException{
        if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // permission is granted
            LocationManager locationManager;
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, this);
        }
    }

    @Override
    public void onLocationChanged(Location location) {
        //the Location object this method brings has the location data in it
        double value1 = location.getLatitude();
        double value2 = location.getLongitude();
        TextView tv = (TextView) findViewById(R.id.gpsTextView);
        tv.setText(getString(R.string.latlonString, String.valueOf(value1), String.valueOf(value2)));
    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(MainActivity.this, "gps is activated.....", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(MainActivity.this, "gps is NOT activated.....", Toast.LENGTH_LONG).show();
    }
}