本文介绍了Android的LocationLister在IntentService实现从未执行OnLocationChanged()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写它实现了LocationListener的接口的IntentService。该服务应发送邮件的时候OnLocationChanged()方法被调用的第一次。但OnLocationChanged()不会被调用。这是我的code:

公共类MyIntentService扩展IntentService实现LocationListener的{    私人诠释结果= Activity.RESULT_CANCELED;    保护使者mMessenger;    保护LocationManager mLocationManager = NULL;    公共MyIntentService(){        超级(LocationService);    }    @覆盖    保护无效onHandleIntent(意向意图){        Log.i(TAG,得到了首发的意图:+ intent.toString());        捆绑额外= intent.getExtras();        如果(临时演员!= NULL){            信使=(信使)extras.get(信使);            如果(mLocationManager == NULL){            mLocationManager =(LocationManager)MyIntentService.this                                    .getSystemService(Context.LOCATION_SERVICE);            }            字符串提供商= LocationManager.GPS_PROVIDER;            布尔gpsIsEnabled = mLocationManager.isProviderEnabled(供应商);            如果(gpsIsEnabled){                上下文CON = MyIntentService.this;                mLocationManager.requestLocationUpdates(提供商,0,0,                                                        MyIntentService.this);            }            其他 {                Log.i(TAG,没有GPS!);            }        }    @覆盖    公共无效onLocationChanged(位置定位){        Log.i(TAG,得到的位置);        Log.i(TAG,GOT使者:+ mMessenger.toString()                    +\ n和位置管理:+ mLocationManager.toString());        如果(mMessenger = NULL和放大器;!&安培;!mLocationManager = NULL){            结果= Activity.RESULT_OK;            消息味精= Message.obtain();            msg.arg1 =结果;            msg.obj =位置;            mLocationManager.removeUpdates(MyIntentService.this);            尝试{mMessenger.send(MSG); }            赶上(RemoteException的E){                Log.i(TAG,捕获到异常:+ e.getMessage());            }        }    }    @覆盖    公共无效onProviderDisabled(字符串提供商){}    @覆盖    公共无效onProviderEnabled(字符串提供商){}    @覆盖    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){}}

任何人有一个想法?

解决方案

这是不是一个好主意。一个 IntentService 不应该做任何事情,生活越过 onHandleIntent()方法。

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

如果你的目标是有一个合理的当前位置在任何时候,试试小蓬松的位置库的。或者,考虑我的 LocationPoller

I've written an IntentService which implements the LocationListener interface. The service should send a message when OnLocationChanged() method was called at the first time.But OnLocationChanged() is never called.Here's my code:

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) {}

}

Anyone has an idea?

解决方案

That is not a good idea. An IntentService should not do anything that lives past the onHandleIntent() method.

This is not surprising, as the service will be destroyed a millisecond or so after it is created, based upon your implementation.

If your objective is to have a reasonably current location at all times, try the Little Fluffy Location Library. Or, consider my LocationPoller.

这篇关于Android的LocationLister在IntentService实现从未执行OnLocationChanged()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 06:51