Android在IntentService中使用LocationListener是一个好习惯吗?

时间:2014-12-11 15:11:45

标签: android intentservice locationlistener

我的应用程序有一个小部件,并根据设备的位置显示信息。

我想通过使用IntentService获取位置,因为它在作业完成后会自行销毁,但是algorythm会执行

@Override
protected void onHandleIntent(Intent intent) 

方法和完成。所以没有时间听一些地点并回复信息。

是否可以让LocationListener等到

@Override
public void onLocationChanged(Location location) {

被称为?

如何正确使用

中的Looper
LocationManager.requestLocationUpdates(

方法

这里是IntentService的整个代码:

public class GetLocation extends IntentService {
public GetLocation() {
    super("GetLocation");
    // TODO Auto-generated constructor stub
}

private static final String TAG = "GetLocation";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private ResultReceiver resultReceiver;
public static final String RECEIVER = "receiver";
public static final String GPS = "gps";
private int result = Activity.RESULT_CANCELED;
public static String RESULT = "result";
Location mLastLocation;

private class LocationListener implements android.location.LocationListener {

    public LocationListener(String provider) {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation.set(location);
        result = Activity.RESULT_OK;
        publishResults(new double[] { mLastLocation.getLatitude(),
                mLastLocation.getLongitude() }, result);

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}

LocationListener[] mLocationListeners = new LocationListener[] {
//          new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER) };

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    Log.e(TAG, "onHandleIntent");
    resultReceiver = intent.getParcelableExtra(RECEIVER);

    initializeLocationManager();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL,
                LOCATION_DISTANCE, mLocationListeners[0]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    }


}


@Override
public void onDestroy() {
    Log.e(TAG, "onDestroy");
    super.onDestroy();
    if (mLocationManager != null) {
        for (int i = 0; i < mLocationListeners.length; i++) {
            try {
                mLocationManager.removeUpdates(mLocationListeners[i]);
                Log.i(TAG, "remove location listners");
            } catch (Exception ex) {
                Log.i(TAG, "fail to remove location listners, ignore", ex);
            }
        }
    }
}

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
    }
}

private void publishResults(double[] gps, int result) {

    Bundle bundle = new Bundle();
    bundle.putDoubleArray(GPS, gps);
    bundle.putInt(RESULT, result);
    resultReceiver.send(Activity.RESULT_OK, bundle);
}
}

编辑:

我想为服务添加最终代码,为接收者提供坐标:

但是,只有gps提供商正常工作,设备上的网络被忽略(一切都在设置中被激活)

public class LocationGetter extends Service {

private static final String TAG = "LocationGetter";
private LocationManager mLocationManager = null;
private static final int LOCATION_INTERVAL = 1000;
private static final float LOCATION_DISTANCE = 10f;
private ResultReceiver resultReceiver;
public static final String RECEIVER = "receiver";
public static final String GPS = "gps";
private int result = Activity.RESULT_CANCELED;
public static String RESULT = "result";
Location mLastLocation;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    Log.e(TAG, "onStartCommand");
    resultReceiver = intent.getParcelableExtra(RECEIVER);

    initializeLocationManager();
    try {
        mLocationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL,
                LOCATION_DISTANCE, mLocationListeners[0]);
    } catch (java.lang.SecurityException ex) {
        Log.i(TAG, "fail to request location update, ignore", ex);
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "network provider does not exist, " + ex.getMessage());
    }

    return super.onStartCommand(intent, flags, startId);
}

private class LocationListener implements android.location.LocationListener {

    public LocationListener(String provider) {
        Log.e(TAG, "LocationListener " + provider);
        mLastLocation = new Location(provider);
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.e(TAG, "onLocationChanged: " + location);
        mLastLocation.set(location);
        result = Activity.RESULT_OK;
        publishResults(new double[] { mLastLocation.getLatitude(),
                mLastLocation.getLongitude() }, result);

    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.e(TAG, "onProviderDisabled: " + provider);
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.e(TAG, "onProviderEnabled: " + provider);
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.e(TAG, "onStatusChanged: " + provider);
    }
}

LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER) };

private void initializeLocationManager() {
    Log.e(TAG, "initializeLocationManager");
    if (mLocationManager == null) {
        mLocationManager = (LocationManager) getApplicationContext()
                .getSystemService(Context.LOCATION_SERVICE);
    }
}

private void publishResults(double[] gps, int result) {

    Bundle bundle = new Bundle();
    bundle.putDoubleArray(GPS, gps);
    bundle.putInt(RESULT, result);
    resultReceiver.send(Activity.RESULT_OK, bundle);
}
}

1 个答案:

答案 0 :(得分:0)

您选择了错误的方法 - 您应该使用服务来侦听位置更新,因为服务在执行完代码后不会关闭。

另一种方法 - 通过定义正确的IntentFilter,通过AndroidManifest.xml将一些服务组件订阅到位置更新。在这种情况下 - 它可以是一个IntentService,因为它只会执行它的OnReceive()方法。