Page Summary
-
The Snapshot API can be used to get the current state for supported context types.
-
The Places and Weather contextual signals have been deprecated and are no longer available for new implementations.
-
You can get the user's current activity using
getDetectedActivity(), which requires theACTIVITY_RECOGNITIONpermission. -
To get information about nearby beacons, use
getBeaconState(), which requires theACCESS_FINE_LOCATIONpermission and activation of the Nearby Messages API. -
You can detect if headphones are plugged in using
getHeadphoneState(), which returns a state of eitherPLUGGED_INorUNPLUGGED.
This section shows how to use the Snapshot API to get the current state for each of the supported context types. For more information, see Get started . For details on deprecated contextual signals, open the following expandable notice:
Get the current activity
To get the user's current activity, call getDetectedActivity()
, which returns an ActivityRecognitionResult
that contains information about the user's most recent activities.
The getDetectedActivity()
method requires the com.google.android.gms.permission.ACTIVITY_RECOGNITION
permission. Add this
permission to AndroidManifest.xml
.
To get the user's current activity, perform the following steps:
- Call
getSnapshotClient()to create an instance of theSnapshotClient. - Use
addOnSuccessListenerto create anOnSuccessListenerthat can listen for aDetectedActivityResponse. - Call
getStatus()to ensure that the result is valid. -
Call
DetectedActivityResponse.getActivityRecognitionResult()to return anActivityRecognitionResult. You can use this to get many aspects of the user's current activity. For example:- Call
getMostProbableActivity()to get only the most probable activity. - Call
getProbableActivities()to get a list of recent activities ranked by probability. - Call
getActivityConfidence()to return the confidence value for a given activity type. - Call
hasResult()to detect whether anIntentcontains anActivityRecognitionResult.
- Call
The following code example uses getMostProbableActivity()
to get the most probable detected activity, and to log the result to the
console:
Awareness
.
getSnapshotClient
(
this
).
getDetectedActivity
()
.
addOnSuccessListener
(
new
OnSuccessListener<DetectedActivityResponse>
()
{
@Override
public
void
onSuccess
(
DetectedActivityResponse
dar
)
{
ActivityRecognitionResult
arr
=
dar
.
getActivityRecognitionResult
();
DetectedActivity
probableActivity
=
arr
.
getMostProbableActivity
();
int
confidence
=
probableActivity
.
getConfidence
();
String
activityStr
=
probableActivity
.
toString
();
mLogFragment
.
getLogView
().
println
(
"Activity: "
+
activityStr
+
", Confidence: "
+
confidence
+
"/100"
);
}
}
)
Get nearby beacons
To get information about nearby beacons
, call getBeaconState()
.
Beacon data consists of the content, type, and namespace of any attachments.
The getBeaconState()
method requires the android.permission.ACCESS_FINE_LOCATION
permission. Add this permission to AndroidManifest.xml
.
In addition, you must activate the Nearby Messages API for your Google Developers Console
project. For more information, see Signup and API Keys
and Get started
.
To get information about nearby beacons, perform the following steps:
-
Check whether the user has granted the required permissions. The following example checks to see whether the
android.permission.ACCESS_FINE_LOCATIONpermission is granted. If not, the user is prompted for consent.if ( ContextCompat . checkSelfPermission ( MainActivity . this , Manifest . permission . ACCESS_FINE_LOCATION ) != PackageManager . PERMISSION_GRANTED ) { ActivityCompat . requestPermissions ( MainActivity . this , new String []{ Manifest . permission . ACCESS_FINE_LOCATION }, MY_PERMISSION_LOCATION ); return ; } -
Define a
BeaconState.TypeFilter. This returns only beacons with attachments that are registered with the specified namespace and type. You can also filter based on a byte-for-byte match on the attachment content. The following example shows how to create a type filter:private static final List < BeaconState . TypeFilter > BEACON_TYPE_FILTERS = Arrays . asList ( BeaconState . TypeFilter . with ( "my.beacon.namespace" , "my-attachment-type" ), BeaconState . TypeFilter . with ( "my.other.namespace" , "my-attachment-type" )); -
Use
addOnSuccessListenerto create anOnSuccessListenerthat can listen for aBeaconStateResponse. -
Call
getStatus()to ensure that the result is valid. -
Call
BeaconStateResponse.getBeaconState()to return the beacon state. -
Call
BeaconState.getBeaconInfo()to get aBeaconState.BeaconInfo.
The following example shows how to get beacon info:
Awareness
.
getSnapshotClient
(
this
).
getBeaconState
(
BEACON_TYPE_FILTERS
)
.
addOnSuccessListener
(
new
OnSuccessListener<BeaconStateResponse>
()
{
@Override
public
void
onSuccess
(
BeaconStateResponse
beaconStateResponse
)
{
BeaconStateResult
beaconStateResult
=
beaconStateResponse
.
getBeaconState
();
BeaconState
.
BeaconInfo
beaconInfo
=
beaconStateResponse
.
getBeaconInfo
();
}
}
)
Get headphone state
To detect whether headphones are plugged into the device, call getHeadphoneState()
,
which creates a HeadphoneStateResponse
detect state with OnSuccessListener
set to detect.
You can then call getHeadphoneState()
to get the HeadphoneState
.
To get the current headphone state, perform the following steps:
- Call
getSnapshotClient.getHeadphoneState(). - Use
addOnSuccessListenerto create anOnSuccessListenerthat can listen for aHeadphoneStateResponse. - Call
getStatus()to ensure that the result is valid. - On success, call
HeadphoneStateResponse.getHeadphoneState()to return the headphone state. This value is eitherPLUGGED_INorUNPLUGGED.
The following code example shows how to use getHeadphoneState()
:
Awareness
.
getSnapshotClient
(
this
).
getHeadphoneState
()
.
addOnSuccessListener
(
new
OnSuccessListener<HeadphoneStateResponse>
()
{
@Override
public
void
onSuccess
(
HeadphoneStateResponse
headphoneStateResponse
)
{
HeadphoneState
headphoneState
=
headphoneStateResponse
.
getHeadphoneState
();
boolean
pluggedIn
=
headphoneState
.
getState
()
==
HeadphoneState
.
PLUGGED_IN
;
String
stateStr
=
"Headphones are "
+
(
pluggedIn
?
"plugged in"
:
"unplugged"
);
mLogFragment
.
getLogView
().
println
(
stateStr
);
}
}
)
.
addOnFailureListener
(
new
OnFailureListener
()
{
@Override
public
void
onFailure
(
@NonNull
Exception
e
)
{
Log
.
e
(
TAG
,
"Could not get headphone state: "
+
e
);
}
}
);
Get location
You can get the user's current location (latitude-longitude) with a call to getLocation()
, which returns a LocationResponse
.
You can then call LocationResponse.getLocation()
to get a Location
with the current location data.
The getLocation()
method requires the android.permission.ACCESS_FINE_LOCATION
permission. Add this permission to AndroidManifest.xml
.
To get the current location, perform the following steps:
-
Check whether the user has granted the required permissions. The following example checks to see whether the
android.permission.ACCESS_FINE_LOCATIONpermission has been granted. If not, the user is prompted for consent.if ( ContextCompat . checkSelfPermission ( MainActivity . this , Manifest . permission . ACCESS_FINE_LOCATION ) != PackageManager . PERMISSION_GRANTED ) { ActivityCompat . requestPermissions ( MainActivity . this , new String []{ Manifest . permission . ACCESS_FINE_LOCATION }, MY_PERMISSION_LOCATION ); return ; } -
Use
addOnSuccessListenerto create anOnSuccessListenerthat can listen for aLocationResponse. -
Call
getStatus()to ensure that the result is valid. -
Call
LocationResponse.getLocation()to return the currentLocation.
The following example shows how to get the current location:
Awareness
.
getSnapshotClient
(
this
).
getLocation
()
.
addOnSuccessListener
(
new
OnSuccessListener<LocationResponse>
()
{
@Override
public
void
onSuccess
(
LocationResponse
locationResponse
)
{
Location
loc
=
locationResponse
.
getLocationResult
();
}
}
)

