我创建了一个实现LocationListener并使用融合位置提供程序的服务。我的问题是我想将服务中的onLocationChanged的gps坐标发送到更新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")
}
};
... }
答案 0 :(得分: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。我还不明白为什么你目前的代码不起作用?
答案 1 :(得分:0)
尝试从您的活动中执行runOnUiThread()。