2013-07-13 18 views
1

我創建了一個實現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") 
     } 
}; 

... }

+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); 
    } 

你可能會發現這種方法很簡單R'那麼你所需要做的就是調用update gui。我也不明白的是爲什麼你當前的代碼不工作?

+0

我已經上傳了部分代碼。如果不使用服務什麼是更好的方法?問題是我想將數據發送到多個活動。那麼,我該怎麼做,如果我不使用服務? – user2177273

+0

當前的代碼正在工作。問題是我添加了一個整數來計算onLocationChanged方法中插入的新位置的數量,並且我意識到lat例如在UI中改變了2次,整數是10,這意味着從未顯示的8個座標在UI – user2177273

+0

嗯這就是奇怪的,你可以發佈該代碼? –

0

嘗試從您的活動中執行runOnUiThread()。

相關問題