开发者

Timing problem when updating Activity with Geoposition from a Service (using Shared Preferences to transmit the geoposition from Service to Activity)

In my App an Activity starts a Service. The Service obtains network-based geoposition in its onLocatioChanged method, and writes it to Shared Preferences. Then calls its own stopSelf(); method. Back in my Activity, I read the geoposition from Shared Prefs again.

My code actually works, the only problem is that the geoposition that my Activity reads out from the Shared Prefs, is always from the privious start of the service. The Service needs some milliseconds to obtain the geoposition- during that time the Activity has read the outdated (previous) geoposition. I've searched the forum, tried to start the Servive in a new thread and introduced waiting time in the Activity Thread using Thread.sleep(). Nothing worked so far.

The relevant code from my Acivity to start GeoServce.class, and read Shared Prefs:

btnGeoPos.setOnLongClickListener (new View.OnLongClickListener(){
            public boolean onLongClick(View v) {

            startService(new Intent(getApplicationContext(), GeoService.class));

                SharedPreferences mPrefs = getApplicationContext().getSharedPreferences("CurrentLoc", MODE_PRIVATE);
             DisplayLoc = mPrefs.getString("LocationTransfer", "not available");
             Toast.makeText(getApplicationContext(),"  GeoService retrieved:  "+DisplayLoc, Toast.LENGTH_LONG).show();

            return true;  //LongClick was consumed                   
    }          
    });

This is my Service class that writes the geoposition into SharedPrefs, it works fine per se:

public class GeoService extends Service {

 Location location = null;
 LocationManager lm;
 LocationListener ll;

 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }  



    public void onCreate() {           
        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        ll = new MyLocationListener();         
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,1000, ll);    
        }    



    public void onStart(Intent intent, int startiD){}

 private class MyLocationListener implements LocationListener {

  public void onLocationChanged(Location location) {
   String message = String.format("Current Location \n Longitude: %1$s \n Latitude: %2$s",location.getLongitude(), location.getLatitude());
   Toast.makeText(GeoService.this, message, Toast.LENGTH_LONG).show();

   SharedPreferences mPrefs = GeoService.this.g开发者_StackOverflowetSharedPreferences("CurrentLoc", MODE_PRIVATE);  
            SharedPreferences.Editor editor1 = mPrefs.edit();
     editor1.putString("LocationTransfer", message); // LocationTransfer is the key, message is the content passed    
     editor1.commit();

      lm.removeUpdates(ll);
   stopSelf();
  }

  public void onStatusChanged(String s, int i, Bundle b) {}

  public void onProviderDisabled(String s) {
   Toast.makeText(GeoService.this,"Network Location turned off",Toast.LENGTH_LONG).show();
  }

  public void onProviderEnabled(String s) {}

 }// End inner class

}// End Class

This roadblock keeps me searching since weeks, I would appreciate any help, thanks to all!


You have several options:

  1. Make you approach asynchronous: let Service notify Activity when it's done. You can use BroadcastReceiver to achieve this.

  2. If this service is short-lived and only used internally by your activity, then you don't need a Service. Use AsyncTask instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜