Android LocationUpdate不工作
我正在尝试实现位置更新侦听器来检测android中的位置更改。我通过日志GPS收到通知。但在此之后,onLocationChanged函数再也不会调用。我通过改变位置进行测试,但它从未调用。 请注意,GPS已启用Android LocationUpdate不工作,android,geolocation,android-location,Android,Geolocation,Android Location,我正在尝试实现位置更新侦听器来检测android中的位置更改。我通过日志GPS收到通知。但在此之后,onLocationChanged函数再也不会调用。我通过改变位置进行测试,但它从未调用。 请注意,GPS已启用 public class locationActivity extends Activity implements LocationListener{ LocationManager lManager=null; TextView _textview; void ini
public class locationActivity extends Activity implements LocationListener{
LocationManager lManager=null;
TextView _textview;
void initLocationService()
{
{
lManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
GpsStatus.Listener gpsListener=new GpsStatus.Listener() {
@Override
public void onGpsStatusChanged(int event) {
if(event==GpsStatus.GPS_EVENT_STOPPED)
{
Log.e("GPS Listener","GPS Stoped");
}
else if(event==GpsStatus.GPS_EVENT_STARTED)
{
Log.e("GPS Listener","GPS Started");
}
}
};
LocationRequest lrequest=new LocationRequest();
lManager.addGpsStatusListener(gpsListener);
}
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e("GPS Listener","LocationChanged");
_textview.setText("LocationChanged"+count);
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
Log.e("GPS Listener","StatusChanged");
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS Listener","ProvidedEnabled");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Log.e("GPS Listener","ProviderDisabled");
}
enter code here
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.location_test);
_textview=(TextView)findViewById(R.id.textView1);
initLocationService();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
lManager.removeUpdates(this);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 0,this);
}
}要获取位置更新,您需要调用RequestLocationUpdate并为其提供一个LocationListener 通常,您会使用以下内容定义and:
LocationManager locMgr = null;
LocationListener locListener = null;
locListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null) {
// Do something
}
}
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int status, Bundle extras) { }
};
并在onResume中通过以下调用启用它:
locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0, // min time in ms
0, // min distance in meters
locListener);
在暂停时:
locMgr.removeUpdates(locListener);