我一直試圖通過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在android中通過GPS訪問位置
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);
}
}
我建議使用FusedLocationProviderApi。這是更準確的,並會解決很多事情給你。 https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi –
好吧,我也會檢查一下,但你能幫我找到這個代碼中的問題.. –