在 IntentService 中实现的 Android LocationLister 从不执行 OnLocationChanged() 方法

标签 android intentservice locationlistener

我编写了一个实现 LocationListener 接口(interface)的 IntentService。首次调用 OnLocationChanged() 方法时,服务应发送一条消息。 但是永远不会调用 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) {}

}

有人有想法吗?

最佳答案

I've written an IntentService which implements the LocationListener interface.

这不是一个好主意。 IntentService 不应该做任何超过 onHandleIntent() 方法的事情。

The service should send a message when OnLocationChanged() method was called at the first time. But OnLocationChanged() is never called.

这并不奇怪,因为根据您的实现,服务将在创建后一毫秒左右被销毁。

如果您的目标是始终拥有合理的当前位置,请尝试 Little Fluffy Location Library .或者,考虑我的 LocationPoller .

关于在 IntentService 中实现的 Android LocationLister 从不执行 OnLocationChanged() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10319466/

相关文章:

android - 如何防止多次调用 onHandleIntent() - IntentService

Android按时间间隔发送经纬度,位置对象

android - 如何在 Android 系统跟踪中添加自定义标记?

android - 如何在软键盘中获得 "done"按钮?

java - Android 操作栏覆盖并默认隐藏在 xml 中

android - React PWA 无法在 Chrome 中打开外部 PDF(Android)

Android停止指定的IntentService

android - IntentService 工作队列状态

android - LocationClient 在屏幕灯熄灭时不提供回调,但我的 WakefulThread 按预期完美运行

android - 在 GPS 中获取最近的地方与我在 android 中的当前位置进行比较