7
  • Làm thế nào để lấy lại vị trí hiện tại bằng cách sử dụng mới FusedLocation API từ một IntentService?
  • Làm thế nào tôi có thể chắc chắn rằng IntentService sẽ nhận được tất cả callbacks từ FusedLocation API? tức là, Làm cách nào để có được đối tượng vị trí đồng bộ?
+5

Bắt đầu bằng cách thay thế 'IntentService' với một dịch vụ thường xuyên. 'IntentService' không xử lý các API gọi là chính chúng không đồng bộ, như API nhà cung cấp vị trí hợp nhất. Bạn sẽ cần một dịch vụ thường xuyên, với chủ đề nền của riêng bạn (khi cần), nơi bạn có thể tắt dịch vụ chỉ khi bạn nhận được dữ liệu của bạn (hoặc một số thời gian chờ nó đạt được). "Làm cách nào tôi có thể lấy được đối tượng vị trí một cách đồng bộ?" - không có gì đảm bảo rằng bạn có thể, đó là lý do tại sao bạn thay thế 'IntentService' bằng một dịch vụ thông thường. – CommonsWare

+0

@DroidHacker bạn đã giải quyết vấn đề này chưa? – rup35h

Trả lời

4

này có thể giúp bạn

import java.util.ArrayList; 
    import java.util.List; 

    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONArray; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.app.AlertDialog; 
    import android.app.Notification; 
    import android.app.NotificationManager; 
    import android.app.PendingIntent; 
    import android.app.Service; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.location.Criteria; 
    import android.location.Location; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.os.Handler; 
    import android.os.IBinder; 
    import android.util.Log; 
    import android.widget.Toast; 

    import com.example.driverapplication.CommonUtilities; 
    import com.example.driverapplication.R; 
    import com.example.driverapplication.ServiceHandler; 
    import com.example.driverapplication.utilities.ConnectionDetector; 
    import com.google.android.gms.common.ConnectionResult; 
    import com.google.android.gms.common.api.GoogleApiClient; 
    import com.google.android.gms.location.LocationRequest; 
    import com.google.android.gms.location.LocationServices; 

    public class LocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 
     private static final String TAG = "DRIVER"; 
     private LocationManager mLocationManager = null; 
     private static final int LOCATION_INTERVAL = 30000; 
     private static final float LOCATION_DISTANCE = 0; 
     private double currentLat, currentLng; 
     private SharedPreferences pref; 
     private String driverId; 
     private GoogleApiClient mGoogleApiClient; 
     // A request to connect to Location Services 
     private LocationRequest mLocationRequest; 

     private LocationListener locationListener; 

     private class LocationListener implements 
       com.google.android.gms.location.LocationListener { 

      public LocationListener() { 
      } 

      @Override 
      public void onLocationChanged(Location location) { 
       Log.e(TAG, "onLocationChanged: " + location); 
       currentLat = location.getLatitude(); 
       currentLng = location.getLongitude(); 

      } 


     } 


     @Override 
     public IBinder onBind(Intent arg0) { 
      return null; 
     } 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 
      Log.e(TAG, "onStartCommand"); 
      super.onStartCommand(intent, flags, startId); 
      boolean stopService = false; 
      if (intent != null) 
       stopService = intent.getBooleanExtra("stopservice", false); 

      System.out.println("stopservice " + stopService); 

      locationListener = new LocationListener(); 
      if (stopService) 
       stopLocationUpdates(); 
      else { 
       if (!mGoogleApiClient.isConnected()) 
        mGoogleApiClient.connect(); 
      } 

      return START_STICKY; 
     } 

     @Override 
     public void onCreate() { 
      Log.e(TAG, "onCreate"); 
      pref = getSharedPreferences("driver_app", MODE_PRIVATE); 
      driverId = pref.getString("driver_id", ""); 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API).addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this).build(); 
     } 



     @Override 
     public void onDestroy() { 
      Log.e(TAG, "onDestroy"); 
      super.onDestroy(); 
     } 

     public void stopLocationUpdates() { 
      LocationServices.FusedLocationApi.removeLocationUpdates(
        mGoogleApiClient, locationListener); 

      if (mGoogleApiClient.isConnected()) 
       mGoogleApiClient.disconnect(); 
     } 


     @Override 
     public void onConnectionFailed(ConnectionResult arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onConnected(Bundle arg0) { 
      // TODO Auto-generated method stub 
      mLocationRequest = LocationRequest.create(); 
      mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
      mLocationRequest.setInterval(35000); 
      mLocationRequest.setFastestInterval(30000); 
      startLocationUpates(); 
     } 
     private void startLocationUpates() { 
      LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, mLocationRequest, locationListener); 
     } 


     @Override 
     public void onConnectionSuspended(int arg0) { 
      // TODO Auto-generated method stub 

     } 

    } 
+0

Xin lưu ý rằng Dịch vụ (trái ngược với IntentService) hoạt động trong chuỗi chính của ứng dụng. Bằng cách không tạo chủ đề của riêng bạn trong dịch vụ này, bạn sẽ chặn luồng giao diện người dùng ứng dụng có thể dẫn đến trải nghiệm người dùng kém. – lenrok258

Các vấn đề liên quan