Я написал IntentService, который реализует интерфейс LocationListener. Служба должна отправить сообщение, когда метод OnLocationChanged() был вызван в первый раз. Но OnLocationChanged() никогда не вызывается. Вот мой код:Android LocationLister, реализованный в IntentService, никогда не выполняет метод OnLocationChanged()
public class MyIntentService extends IntentService implements LocationListener {
private int result = Activity.RESULT_CANCELED;
protected Messenger mMessenger;
protected LocationManager mLocationManager = null;
public MyIntentService() {
super("LocationService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "Got starting intent: " + intent.toString());
Bundle extras = intent.getExtras();
if (extras != null) {
Messenger = (Messenger) extras.get("MESSENGER");
if(mLocationManager == null) {
mLocationManager = (LocationManager) MyIntentService.this
.getSystemService(Context.LOCATION_SERVICE);
}
String provider = LocationManager.GPS_PROVIDER;
boolean gpsIsEnabled = mLocationManager.isProviderEnabled(provider);
if(gpsIsEnabled) {
Context con = MyIntentService.this;
mLocationManager.requestLocationUpdates(provider, 0, 0,
MyIntentService.this);
}
else {
Log.i(TAG, "NO GPS!");
}
}
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "Got Location");
Log.i(TAG, "Got Messenger: " + mMessenger.toString()
+ "\nand Location Manager: " + mLocationManager.toString());
if(mMessenger != null && mLocationManager != null){
result = Activity.RESULT_OK;
Message msg = Message.obtain();
msg.arg1 = result;
msg.obj = location;
mLocationManager.removeUpdates(MyIntentService.this);
try { mMessenger.send(msg); }
catch (RemoteException e) {
Log.i(TAG, "Exception caught : " + e.getMessage());
}
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
Каждый имеет идея?
Вы уверены, что получаете LocationManager OK? Вы можете попробовать 'Context.getSystemService (Context.LOCATION_SERVICE);' –
также убедитесь, что у вас есть правильные разрешения, установленные в манифесте, и попробуйте разрешить разрешенным провайдерам устранить проблему, то есть разрешить обновления сети и wifi и т. Д. И т. Д. И т. Д. Посмотрите, что происходит. Проверьте жизненный цикл Сервиса и убедитесь, что он не уничтожается после того, как onHandleIntent был выполнен. –
Испытываете ли вы это на реальном устройстве? Сначала проверьте, правильно ли запущена служба ... Затем попробуйте изменить расстояние до 1 или более в вызове 'requestLocationUpdates'. – Ronnie