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