我有这个问题。我正在获取位置更新。它在我的模拟器上运行良好。但是当我在我的设备上测试它时,它显示当前位置很好。但是当我改变我的位置时,它不会显示我的位置更新。但它在模拟器上运行良好。这是我的代码 Main.java
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
TextView tvStatus;
LocationManager lm;
boolean gpsEnabled;
LocationListener ls = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onLocationChanged(Location location) {
tvStatus.setText("Latitude: "
+ location.getLatitude() + "\nLongitude: "
+ location.getLongitude());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "on create", 300).show();
tvStatus = (TextView) findViewById(R.id.textView1);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "on resume", 200).show();
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
tvStatus.setText(l.getLatitude()+" "+ l.getLongitude());
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ls);
}else{
//use network provider
Toast.makeText(this, "dsfdsfdsfdsf", 300).show();
}
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this,"on pause" ,300).show();
lm.removeUpdates(ls);
}
}
我在 manifest.xml 中添加了所有权限