Android定位服务:我可以为GPS和网络位置使用单个位置监听器吗?

时间:2012-05-25 17:05:16

标签: android geolocation location android-location

我是否为网络位置和gps位置注册了一个locationlistener对象,还是为每个位置创建一个单独的位置?

2 个答案:

答案 0 :(得分:2)

您可以为两者使用相同的侦听器。

当您获得onLocationChanged()onStatusChanged()回调时,您可以检查传入的参数(位置或提供商)以确定回调的来源(即:网络或GPS)。

答案 1 :(得分:1)

使用以下类检索位置。它将选择最佳提供商并返回经度和纬度:

 package com.test.location;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class LocationPicker implements LocationListener, OnCancelListener{

private Context ctx;
private LocationManager locationMgr;
private boolean stopFlag;
private ProgressDialog dialog;

public LocationPicker(Context ctx) {
    this.ctx = ctx;
}

public void retrieveLocation() {

    String locCtx = Context.LOCATION_SERVICE; 

    locationMgr = (LocationManager) ctx.getSystemService(locCtx);

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    String provider = locationMgr.getBestProvider(criteria, true);

    locationMgr.requestLocationUpdates(provider, 0, 0, this);

    Runnable showWaitDialog = new Runnable() {

        @Override
        public void run() {
            while (!stopFlag) {
                // Wait for first GPS Fix (do nothing until loc != null)
            }
            // After receiving first GPS Fix dismiss the Progress Dialog
            dialog.dismiss();
        }
    };

    dialog = ProgressDialog.show(ctx, "Please wait...", "Retrieving GPS data...", true);
    dialog.setCancelable(true);
    dialog.setOnCancelListener(this);
    Thread t = new Thread(showWaitDialog);
    t.start();
}

@Override
public void onLocationChanged(Location location) {
    if (location != null) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        stopFlag = true;
        Toast.makeText(ctx, "Latitude : " + latitude + " Longitude : " + longitude , Toast.LENGTH_LONG).show();
    }
    locationMgr.removeUpdates(this);
}

@Override
public void onProviderDisabled(String provider) {
//  Toast.makeText(ctx, "GPS Disabled", Toast.LENGTH_LONG).show();
//
//  Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
//  ctx.startActivity(intent);
}

@Override
public void onProviderEnabled(String provider) {
//  Toast.makeText(ctx, "GPS Enabled", Toast.LENGTH_SHORT).show();
//
//  ctx.startActivity(new Intent(ctx, LocationPickerActivity.class));
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onCancel(DialogInterface dialog) {
    stopFlag = true;
    locationMgr.removeUpdates(this);
}
  }

创建上面的类之后,只需调用下面的方法,它将返回纬度和经度:::

    LocationPicker lp = new LocationPicker(this);
    lp.retrieveLocation();

注意:自定义位置侦听器中还包含进度对话框。