2015-11-16 6 views
5

Google से नमूना स्रोत कोड फ्रंटेंड पर निरंतर स्थान अपडेट को कार्यान्वित करना आसान था, लेकिन मैं अभी भी समझ नहीं पा रहा हूं कि स्पष्ट रूप से फ़्यूज्ड लोकेशन अपडेट FusedLocationApi और लंबित इंटेन्टेंट का उपयोग करके कैसे काम करता है।लंबित इन्टेंट के साथ FusedLocationApi। अपडेट प्राप्त करने में असमर्थ

LocationService वर्ग:

public class LocationService extends IntentService { 
private static final String TAG = LocationService.class.getSimpleName(); 

private static final String ACTION_LOCATION_UPDATED = "location_updated"; 
private static final String ACTION_REQUEST_LOCATION = "request_location"; 

public static IntentFilter getLocationUpdatedIntentFilter() { 
    return new IntentFilter(LocationService.ACTION_LOCATION_UPDATED); 
} 

public static void requestLocation(Context context) { 
    Intent intent = new Intent(context, LocationService.class); 
    intent.setAction(LocationService.ACTION_REQUEST_LOCATION); 
    context.startService(intent); 
} 

public LocationService() { 
    super(TAG); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    String action = intent != null ? intent.getAction() : null; 
    if (ACTION_REQUEST_LOCATION.equals(action)) { 
     onRequestLocation(); 
    } 
    else if(ACTION_LOCATION_UPDATED.equals(action)) { 
     onLocationUpdated(intent); 
    } 
} 

/** 
* Called when a location update is requested. We block until we get a result back. 
* We are using Fused Location Api. 
*/ 
private void onRequestLocation() { 
    Log.v(TAG, ACTION_REQUEST_LOCATION); 
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .build(); 

    // we block here 
    ConnectionResult connectionResult = googleApiClient.blockingConnect(10, TimeUnit.SECONDS); 

    if (connectionResult.isSuccess() && googleApiClient.isConnected()) { 

     Intent locationUpdatedIntent = new Intent(this, LocationService.class); 
     locationUpdatedIntent.setAction(ACTION_LOCATION_UPDATED); 

     // Send last known location out first if available 
     Location location = FusedLocationApi.getLastLocation(googleApiClient); 
     if (location != null) { 
      Intent lastLocationIntent = new Intent(locationUpdatedIntent); 
      lastLocationIntent.putExtra(
        FusedLocationProviderApi.KEY_LOCATION_CHANGED, location); 
      startService(lastLocationIntent); 
     } 

     // Request new location 
     LocationRequest mLocationRequest = new LocationRequest() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     FusedLocationApi.requestLocationUpdates(
       googleApiClient, mLocationRequest, 
       PendingIntent.getService(this, 0, locationUpdatedIntent, 0)); 

     googleApiClient.disconnect(); 
    } else { 
     Log.e(TAG, String.format("Failed to connect to GoogleApiClient (error code = %d)", 
       connectionResult.getErrorCode())); 
    } 
} 

/** 
* Called when the location has been updated & broadcast the new location 
*/ 
private void onLocationUpdated(Intent intent) { 
    Log.v(TAG, ACTION_LOCATION_UPDATED); 

    // Extra new location 
    Location location = 
      intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); 

    if (location != null) { 
     LatLng latLngLocation = new LatLng(location.getLatitude(), location.getLongitude()); 
     LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
    } 
} 
} 

MainActivity कोड गंदा लग रहा है तो मैं एक ड्राइव कड़ी में कोड साझा करेंगे: https://drive.google.com/open?id=0B7QMYFlbkUpOVjkxMUtfenRLXzA

हालांकि, मैं https://gist.githubusercontent.com/allenchi/c9659369c306752c0047/raw/16d6cd8e311013379e55b496d2b6d13347f418d6/gistfile1.txt

से उदाहरण का पालन किया है

उदाहरण में निम्नलिखित भाग वह है जो मैं मुख्य गतिविधि में प्लेसमेंट को समझ नहीं सकता। ब्रॉडकास्ट रिसीवर का उपयोग करके मैं स्थान अपडेट कैसे प्राप्त करूं? मुझे पृष्ठभूमि में चल रहे फ़्यूज़लोकेशन की एक साफ समझ की आवश्यकता होगी और फ्लाई जीपीएस ट्रैकर को लागू करने के लिए मैं इसका उपयोग कैसे कर सकता हूं।

@Override 
protected void onResume() { 
    super.onResume(); 
    if(checkPlayServices()) { 
     LocationService.requestLocation(this); 
     LocalBroadcastManager.getInstance(getActivity()) 
        .registerReceiver(locationReceiver, LocationService.getLocationUpdatedIntentFilter()); 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
    locationReceiver, LocationService.getLocationUpdatedIntentFilter()); 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(locationReceiver); 
} 

private BroadcastReceiver locationReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Location location = intent.getParcelableExtra(FusedLocationProviderApi.KEY_LOCATION_CHANGED); 
     if (location != null) { 
      LatLng latestLocation = new LatLng(location.getLatitude(), location.getLongitude()); 
      // do something with the location 
     } 
    } 
}; 

उत्तर

4

आप नीचे दिए नमूने में जब एप्लिकेशन को पृष्ठभूमि हर 5 सेकंड PRIORITY_HIGH_ACCURACY मोड का उपयोग कर में चलाता स्थान अपडेट प्राप्त कर सकते हैं। आप अपने स्थान के निर्देशांक को इंगित करने वाली अधिसूचनाएं प्राप्त करके इन अपडेट को देखेंगे।

मैं भी रूप में यह कहा गया है here का परीक्षण करने के बाद एप्लिकेशन को सिस्टम द्वारा मारा गया है अपडेट प्राप्त किया जा सकता है कि क्या करना चाहता था:

सार्वजनिक सार PendingResult requestLocationUpdates (GoogleApiClient ग्राहक, LocationRequest अनुरोध, PendingIntent callbackIntent)

यह विधि पृष्ठभूमि उपयोग मामलों के लिए उपयुक्त है, विशेष रूप से स्थान अपडेट प्राप्त करने के लिए, जब भी सिस्टम द्वारा ऐप को मार दिया गया है। ऐसा करने के लिए, एक प्रारंभिक सेवा के लिए लंबित इंटेन्टेंट का उपयोग करें।

यही कारण है कि मैंने गतिविधि के ऑनबैकप्रेस() विधि में System.exit (0) को बुलाया है, जिसे आप निश्चित रूप से छोड़ सकते हैं।

सेवा:

public class LocationService extends IntentService{ 

private static final String INTENT_SERVICE_NAME = LocationService.class.getName(); 

public LocationService() { 
    super(INTENT_SERVICE_NAME); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    if (null == intent) { 
     return; 
    } 

    Bundle bundle = intent.getExtras(); 

    if (null == bundle) { 
     return; 
    } 

    Location location = bundle.getParcelable("com.google.android.location.LOCATION"); 

    if (null == location) { 
     return; 
    } 

    if (null != location) { 
     // TODO: Handle the incoming location 

     Log.i(INTENT_SERVICE_NAME, "onHandleIntent " + location.getLatitude() + ", " + location.getLongitude()); 

     // Just show a notification with the location's coordinates 
     NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
     NotificationCompat.Builder notification = new NotificationCompat.Builder(this); 
     notification.setContentTitle("Location"); 
     notification.setContentText(location.getLatitude() + ", " + location.getLongitude()); 
     notification.setSmallIcon(R.drawable.ic_audiotrack); 
     notificationManager.notify(1234, notification.build()); 
    } 
} 

}

गतिविधि:

public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener{ 

private GoogleApiClient googleApiClient; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .build(); 

} 

@Override 
public void onStart() { 
    super.onStart(); 
    googleApiClient.connect(); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    if (googleApiClient.isConnected()) { 
     googleApiClient.disconnect(); 
    } 
} 

@Override 
public void onBackPressed() { 
    // Check whether you receive location updates after the app has been killed by the system 
    System.exit(0); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    requestLocationUpdates(); 
} 

@Override 
public void onConnectionSuspended(int cause) { 
    googleApiClient.connect(); 
} 

@Override 
public void onConnectionFailed(ConnectionResult result) { 

} 

public void requestLocationUpdates() { 
    LocationRequest locationRequest = new LocationRequest() 
      .setInterval(5 * 1000) 
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    Intent intent = new Intent(this, LocationService.class); 
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, pendingIntent); 
} 

}

प्रकट में अनुमति:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
संबंधित मुद्दे