Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Android Location Services

No description
by

Sven Haiges

on 4 September 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Android Location Services

Sven Haiges
hybris.com/+SvenHaiges
Q&A
techblog.hybris.com
location
services update
android
hybris mobile SDK
android 4.0.3, L15
library & template UI
nfc support
geofencing support
for android
I/O13
Fused Location Provider
Activity Recognition
Geofencing APIs
Google Play
Services
Android 2.2+
Install Google Play Services SDK via SDK Manager
Add dependency to Gradle build file
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'com.google.android.gms:play-services:3.1.36'
}
(call in onResume()...)
public boolean playAvailable() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
...
}
Check before using!
installation
Fused Location
Provider

Activity
Recognition

Geofencing APIs
easy to use
faster, more accurate
less battery drain
Source: https://developers.google.com/events/io/sessions/325337477
Permissions
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hybris.locationdemos"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

...

</manifest>

ACCESS_COARSE_LOCATION
=
location obfuscated to block-level
LocationClient
(onCreate)
mLocationClient = new LocationClient(getActivity(), this, this);




(onStart)
mLocationClient.connect();

(onPause)
mLocationClient.disconnect();

ConnectionCallbacks
OnConnectionFailedListener
LastLocation
Location loc = mLocationClient.getLastLocation();

String msg = "Provider: " + loc.getProvider() + " - " + loc.getLatitude() + "/" + loc.getLongitude() + " - " + loc.getAccuracy() + ". ";
msg += SimpleDateFormat.getTimeInstance().format(loc.getTime());

Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
LastLocation
LocationUpdates
LocationRequest mRequest = LocationRequest.create();
mRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

mRequest.setInterval(5000);
mRequest.setFastestInterval(1000);
if (mLocationClient.isConnected())
mLocationClient.requestLocationUpdates(mRequest, this);

...

@Override
public void onLocationChanged(Location location) {
// Report to the UI that the location was updated
String msg = Double.toString(location.getLatitude()) + ","
+ Double.toString(location.getLongitude());
mTextView.setText(msg);
}
little low-drama
demo
GEOFENCE_TRANSITION_ENTER
GEOFENCE_TRANSITION_EXIT
radius
lat, lon
Geofence work = new Geofence.Builder()
.setCircularRegion(48.149932d, 11.546645d, 500f)
.setRequestId("work")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER + Geofence.GEOFENCE_TRANSITION_EXIT)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();

List<Geofence> fences = new ArrayList<Geofence>();
fences.add(work);

Intent intent = new Intent(getActivity(), GeoFenceService.class);
PendingIntent pi = PendingIntent.getService(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

mLocationClient.addGeofences(fences, pi, this);

OnAddGeofencesResultListener
@Override
public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
// If adding the geofences was successful
if (LocationStatusCodes.SUCCESS == statusCode) {
Log.d("demo", "geofences are now active!");
Toast.makeText(getActivity(), TextUtils.join(", ", geofenceRequestIds) + " were added", Toast.LENGTH_LONG).show();
} else {
Log.d("demo", "geofences error - did not add geofences");
Toast.makeText(getActivity(), TextUtils.join(", ", geofenceRequestIds) + " were NOT added", Toast.LENGTH_LONG).show();
}

for (String id : geofenceRequestIds)
{
Log.d("demo", "Geofence: " + id);
}
// Turn off the in progress flag and disconnect the client
mInProgress = false;
mLocationClient.disconnect();

}
public class GeoFenceService extends IntentService {

...

@Override
protected void onHandleIntent(Intent intent) {
//notification here
Log.d("demo", "GeoFenceService...");

int transitionType = LocationClient.getGeofenceTransition(intent);
List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);


String contentText = "";
contentText += (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) ? "ENTER" : "EXIT";
if (geofences != null && geofences.size() > 0)
{
Geofence fence = geofences.get(0);
contentText += " " + fence.getRequestId();
}

notificationManager.notify(0, getNotification(...));

}

}
100
per app&user
requestId
expiration
ACCESS_FINE_LOCATION
code?
ActivityRecognitionClient
ActivityRecognitionResult
low-power sensors
wakes up device periodically
1 instantiate ActivityRecognitionClient

2 when connected, requestActivityUpdates via PendingIntent

3 IntentService called once Activity Detected
react
IN_VEHICLE
ON_BICYCLE
ON_FOOT
STILL
TILTING
UNKNOWN
if (ActivityRecognitionResult.hasResult(intent))
{
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
DetectedActivity mostProbableActivity = result.getMostProbableActivity();
int confidence = mostProbableActivity.getConfidence();
int activityType = mostProbableActivity.getType();
String activityName = getNameFromType(activityType);

...
}
LocationManager

LocationClient
Google Play
Framework
Lock-In
Déjà vu?
code
Sven Haiges
hybris.com/+SvenHaiges
Full transcript