我創建了一個實現LocationListener並使用融合位置提供程序的服務。我的問題是,我想要將gps座標從服務中的onLocationChanged發送到更新UI的活動。我已經註冊了一個BroadcastReceiver來將數據發送到活動,但由於更新頻繁(每秒)發生,UI不會一直更新。大多數座標永遠不會顯示在用戶界面中。我的問題是哪種方法可以將數據從服務內部的onLocationChanged發送到活動?從服務中的onLocationChanged向UI發送GPS座標的最佳方式是什麼?
這裏是我的代碼:
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")
}
};
... }
發佈您正在嘗試的代碼。 –