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中添加了所有权限
答案 0 :(得分:0)
首先,确保您的gps已启用且信号已锁定/固定在实际设备上。如果问题仍然存在,那么在OnCreate()而不是在类中初始化您的locationlistener。
答案 1 :(得分:0)
如果你在建筑物内,我建议不要使用GPS_PROVIDER,因为gps在建筑物内部不起作用,或者在进行位置修复时需要花费大量时间。