我一直试图通过以下代码通过gps获取位置。当我将提供程序更改为NETWORK_PROVIDER时,我得到更新,但是当我使用GPS_PROVIDER时,我没有收到任何数据或位置。 我已经参考了以下教程 - http://www.viralandroid.com/2015/12/how-to-get-current-gps-location-programmatically-in-android.html 和 https://www.youtube.com/watch?v=QNb_3QKSmMk
package app.com.example.android.locationapp;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
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 implements LocationListener {
TextView lat, longi;
LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat = (TextView) findViewById(R.id.textView2);
longi = (TextView) findViewById(R.id.textView4);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Please enable location permission", Toast.LENGTH_SHORT).show();
return;
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Log.i("MainActivity", "reqLocationUpdates() method called");
}
}
@Override
public void onLocationChanged(Location location) {
Log.i("MainActivity","onLocation executed");
lat.setText(location.getLatitude() + "");
longi.setText(location.getLongitude() + "");
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
return;
}
locationManager.removeUpdates(this);
}
}
答案 0 :(得分:0)
确保您拥有良好的位置权限设置。并确保您正在进行运行时权限检查。但很可能你的问题是卫星的LOS。如果您在室内进行测试,您可能无法获得卫星信号。没有它,你无法弄清楚你的位置,它永远不会打电话给你。到外面去测试,看它是否有效。