Precise and lastknown location - Android Studio¶
Pulse provides two methods for the app to get device location in the foreground: (a) High precision location and (b) Last known location. Please refer to the demo app code to see a complete example.
Request high precision location Use the getHighPrecisionLocation() method to request high precision location
mApplication.getPulseSDKInstance().getHighPrecisionLocation(context);
where context = this.getApplicationContext()
Receive high precision location A BroadcastReceiver can then be used to receive the information asynchronously.
- Add the following BroadcastReceiver to AndroidManifest.xml file
<receiver android:name="com.pulse.sdk.broadcast.receiver.PulsePrecisionLocationReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="com.pulse.sdk.broadcast.high.precision.location.CUSTOM_INTENT"> </action> </intent-filter> </receiver>
-
Then add the “com.pulse.sdk.broadcast.receiver” package in the application project
-
Inside the package, create a “PulsePrecisionLocationReceiver” class
-
Once setup, they should appear as the following directory structure:
-
Please refer to the demo app’s PulsePrecisionLocationReceiver.java file to get the location information:
public class PulsePrecisionLocationReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { double dValue = 0; float fValue = 0; long lValue = 0; double latitude = intent.getDoubleExtra(PulseConstants.PULSE_LOCATION_LATITUDE, dValue); double longitude = intent.getDoubleExtra(PulseConstants.PULSE_LOCATION_LONGITUDE, dValue); float accuracy = intent.getFloatExtra(PulseConstants.PULSE_LOCATION_ACCURACY, fValue); long time = intent.getLongExtra(PulseConstants.PULSE_LOCATION_TIME, lValue); } }
Get last known locaton Use the getLastKnownLocation() method to request the last known location from Pulse’s SDK:
showLastKnownLocation(mApplication.getPulseSDKInstance().getLastKnownLocation(context));
where context = this.getApplicationContext()
private void showLastKnownLocation(ArrayList<String> mLastKnownLocation) { if((null != mLastKnownLocation)&&(mLastKnownLocation.size()==4)) { Toast.makeText(this, "Last-Known-Location:" +PulseConstants.PULSE_LOCATION_LATITUDE+"="+mLastKnownLocation.get(0)+"," +PulseConstants.PULSE_LOCATION_LONGITUDE+"="+mLastKnownLocation.get(1)+"," +PulseConstants.PULSE_LOCATION_ACCURACY+"="+mLastKnownLocation.get(2)+"," +PulseConstants.PULSE_LOCATION_TIME+"="+mLastKnownLocation.get(3), Toast.LENGTH_LONG).show(); }else { Log.i(TAG, "mLastKnownLocation==Null or mLastKnownLocation.size()!=4)"); } }
Regular location update The app can also receive regular location updates from Pulse in all states of operations. A BroadcastReceiver can be used to receive the location information asynchronously.
- Add the following BroadcastReceiver to the AndroidManifest.xml file:
<receiver android:name="com.pulse.sdk.broadcast.receiver.PulseLastKnownLocationReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="com.pulse.sdk.broadcast.last.known.location.CUSTOM_INTENT"> </action> </intent-filter> </receiver>
-
Then add the “com.pulse.sdk.broadcast.receiver” package in the application project
-
Inside the package, create a “PulseLastKnownLocationReceiver” class
-
Once setup, they should appear as the following directory structure:
- Please refer to the demo app’s PulseLastKnownLocationReceiver.java file to get the location information:
public class PulseLastKnownLocationReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { double dValue = 0; float fValue = 0; long lValue = 0; double latitude = intent.getDoubleExtra(PulseConstants.PULSE_LOCATION_LATITUDE, dValue); double longitude = intent.getDoubleExtra(PulseConstants.PULSE_LOCATION_LONGITUDE, dValue); float accuracy = intent.getFloatExtra(PulseConstants.PULSE_LOCATION_ACCURACY, fValue); long time = intent.getLongExtra(PulseConstants.PULSE_LOCATION_TIME, lValue); } }