【问题标题】:What is the best way to send gps coordinates from onLocationChanged inside a service to UI?将 gps 坐标从服务内的 onLocationChanged 发送到 UI 的最佳方法是什么?
【发布时间】:2013-07-13 23:09:40
【问题描述】:

我创建了一个实现 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")
        }
};

... }

【问题讨论】:

  • 发布您正在尝试的代码。

标签: android service


【解决方案1】:

我已经做过一些这样的事情,我可以提出两件事可能会有所帮助。一种方法(这就是我所做的)。是为你想要的东西声明静态变量,比如 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。我也不明白为什么你当前的代码不起作用?

【讨论】:

  • 我已经上传了我的部分代码。如果不使用服务有什么更好的方法?问题是我想将数据发送到多个活动。那么,如果我不使用服务,我该怎么做呢?
  • 当前代码正在运行。问题是我添加了一个整数来计算在 onLocationChanged 方法中插入的新位置的数量,我意识到纬度,例如在 UI 中更改了 2 次,整数为 10,这意味着 8 个坐标从未显示在用户界面中
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多