我正在尝试从我的Android手机获取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) {
}
}
所以我在手机上构建了应用程序,打开了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();
}
}