1

我创建了一个实现 LocationListener 并使用融合位置提供程序的服务。我的问题是我想将 gps 坐标从服务内的 onLocationChanged 发送到更新 UI 的活动。我已经注册了一个 BroadcastReceiver 来将数据发送到活动,但是因为更新发生得非常频繁(每秒),所以 UI 不会一直更新。大多数坐标从未出现在 UI 中。我的问题是将数据从服务内部的 onLocationChanged 发送到活动的最佳方式是什么?

这是我的代码:

public class LocationService extends Service implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

LocationClient locationclient;
LocationRequest locationrequest;
private static final String TAG = "LocationService";
public static final String BROADCAST_ACTION =  "com.example.fusedlocation.displayevent";
Intent intent;

@Override
public void onCreate() {
    super.onCreate();
    int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resp == ConnectionResult.SUCCESS) {
        locationclient = new LocationClient(this, this, this);
        locationclient.connect();

    } else {
        Toast.makeText(this, "Google Play Service Error " + resp,
                Toast.LENGTH_LONG).show();

    }
            intent = new Intent(BROADCAST_ACTION);
}


@Override
public void onStart(Intent intent, int startId) {

    if (locationclient != null && locationclient.isConnected()) {

        locationrequest = LocationRequest.create();
        locationrequest.setInterval(2000)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setSmallestDisplacement(0);

        locationclient.requestLocationUpdates(locationrequest, this);

    }

}
    @Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub

    if (locationclient != null && locationclient.isConnected()) {

        locationrequest = LocationRequest.create();
        locationrequest.setInterval(1000)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setSmallestDisplacement(0);

        locationclient.requestLocationUpdates(locationrequest, this);

    }

}
@Override
public void onDestroy() {

    locationclient.removeLocationUpdates(this);
    locationclient.disconnect();

}
@Override
public void onLocationChanged(Location location) {
    ....
    intent.putExtra("Latitude", location.getLatitude());
    intent.putExtra("Longitude", location.getLongitude());
    sendBroadcast(intent, null);
    ...

}

我的主要活动是:

 public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtConnectionStatus = (TextView) findViewById(R.id.txtConnectionStatus);

    txtLocationRequest = (TextView) findViewById(R.id.txtLocationRequest);
    timer = (Chronometer) findViewById(R.id.timer);

    mAddress = (TextView) findViewById(R.id.address);
    mActivityIndicator = (ProgressBar) findViewById(R.id.address_progress);

    mIntentService = new Intent(this, LocationService.class);
    mLocation = new Location("");
    intent= new Intent(this, LocationService.class);
}
...
@Override
public void onResume() {
    super.onResume();       

    registerReceiver(broadcastReceiver, new IntentFilter(LocationService.BROADCAST_ACTION));
}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(broadcastReceiver);      
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Bundle extras = intent.getExtras();
        double latitude = extras.getDouble("Latitude");
        double longitude = extras.getDouble("Longitude");
txtLocationRequest.setText("Latitude: "+ extras.getDouble("Latitude")+"\n"+
        "Longitude: "+ extras.getDouble("Longitude")
        }
};

... }

4

2 回答 2

0

我已经做了一些这样的事情,有两件事我可以建议可能会有所帮助。一种方法(这就是我所做的)。是为你想要的东西声明静态变量,比如 currentLat 和 currentLong。然后在位置更改时更改这些值,然后您可以在主要活动中检查这些值。

或者您可以使用处理程序,如果您真的需要在每次更新位置时都知道

如果你不使用服务可能会更好,因为据我所知,服务仍然在主 ui 线程上处理,所以你不会让你的应用程序更有效率。

好的,我现在看得好多了

在你的服务中,声明

something extends Service{

public static double curlat
public static double curlong

...在

@Override
public void onLocationChanged(Location location) {
    ....
    if(location.hasLatitude()){curLat=location.getLatitude();}
    if(location.hasLongitude()){curLong=location.getLongitude();}

    ..

然后在你的 mainActivity

public void updateGui(){

 txtLocationRequest.setText("Latitude: "+ Service.curLat+"\n"+
    "Longitude: "+ Service.curLong);
    }

您可能会发现这种方法更容易?那么您需要做的就是调用更新 gui。我也不明白为什么你当前的代码不起作用?

于 2013-07-13T23:33:44.790 回答
0

尝试从您的活动中执行 runOnUiThread()。

于 2013-07-16T07:20:05.397 回答