Share
Embed
Transcript
32 slides
-
agenda • Awareness APIͱͳʹ͔ • Awareness APIΛ͏ • ಋೖ •
Snapshot API • Fence API • ॴײ -
ಋೖ • ैདྷͷGoogle Play ServiceܥͷAPIಋೖํ๏ͱಉ༷ 1. Google Developers ConsoleͰAPIΛ༗ޮʹͯ͠ɺAPI Keyऔಘ
2. build.gradleͷdependenciesʹՃ 3. AndroidManifest.xmlʹmeta-dataΛهೖ 4. AndroidManifest.xmlʹඞཁͳPermissionΛՃ -
ͪͳΈʹ… • Google Play ServicesΛؙͬͱೖΕͯɺ Support LibraryೖΕΔͱMethod Count͕ 655536Λ͑Δ orz
• ೋ͚ͭͩͳͷʹMultiDexͳͲ͕ඞཁorz -
9.2.1 Google Play Services 9.2.1Ͱawareness api༻ͷͷ͕Ͱ͖͍ͯͨɻ DPNHPPHMFBOESPJEHNTQMBZTFSWJDFTDPOUFYUNBOBHFS awarenessͰͳ͍… ϝιουΧϯτΛଌΔͱ10252ɻ Google
Play Service AllͰ4ສ͘Β͍ͳͷͰ3ສ͘Β͍ΕͯΔɻ -
Current Activity • ݱࡏͷϢʔβʔͷঢ়ଶ͕औΕΔ w ࣗಈंͱ͔ʹͬͯΔ w ࣗసंʹͬͯΔ w า͍ͯΔ͔ͬͯΔ
w ͬͯΔ w ࢭ·ͬͯΔ w ͍ͯΔ w า͍ͯΔ -
Current Activity Awareness.SnapshotApi.getDetectedActivity(apiClient) .setResultCallback(new ResultCallback<DetectedActivityResult>() { @Override public void onResult(@NonNull
DetectedActivityResult detectedActivityResult) { if (!detectedActivityResult.getStatus().isSuccess()) { Log.e(TAG, "Could not get the current activity."); } ActivityRecognitionResult ar = detectedActivityResult.getActivityRecognitionResult(); DetectedActivity probableActivity = ar.getMostProbableActivity(); showActivity(probableActivity); } }); • SnapshotApi.getDetectedActivityΛݺͿ͚ͩ • DetectedActivityResultʹઌͷใ͕֨ೲ͞Ε͍ͯΔ -
Headphone State Awareness.SnapshotApi.getHeadphoneState(apiClient) .setResultCallback(new ResultCallback<HeadphoneStateResult>() { @Override public void onResult(@NonNull
HeadphoneStateResult headphoneStateResult) { if (!headphoneStateResult.getStatus().isSuccess()) { Log.e(TAG, "Could not get headphone state."); return; } HeadphoneState headphoneState = headphoneStateResult.getHeadphoneState(); showState(headphoneState); } }); • SnapshotApi.getHeadphoneStateΛݺͿ͚ͩ • HeadphoneStateResultʹઌͷใ͕֨ೲ͞Ε͍ͯΔ -
Location Awareness.SnapshotApi.getLocation(apiClient) .setResultCallback(new ResultCallback<LocationResult>() { @Override public void onResult(@NonNull LocationResult
locationResult) { if (!locationResult.getStatus().isSuccess()) { Log.e(TAG, "Could not get location."); return; } Location location = locationResult.getLocation(); showLocation(location); } }); • SnapshotApi.getLocationΛݺͿ͚ͩ • LocationResultʹઌͷใ͕֨ೲ͞Ε͍ͯΔ -
Nearby Places • ۙ͘ͷࢪઃͷใ͕औΕΔ w ۙ͘ʹ͋Δࢪઃͷ w ໊લ w ి൪߸
w Ңܦ w XFCTJUFVSM w SBUJOH w Ձ֨Ϩϕϧ w ͳͲ Awareness APIҎ֎ʹ ผ్ Google Places APIΛ ༗ޮʹ͢Δඞཁ͕͋Δ ͷͰҙɻ -
Nearby Places Awareness.SnapshotApi.getPlaces(apiClient) .setResultCallback(new ResultCallback<PlacesResult>() { @Override public void onResult(@NonNull
PlacesResult placesResult) { if (!placesResult.getStatus().isSuccess()) { Log.e(TAG, "Could not get places."); return; } showPlaces(placesResult); } }); • SnapshotApi.getPlacesΛݺͿ͚ͩ • PlaceResultʹઌͷใ͕֨ೲ͞Ε͍ͯΔ -
Weather • ఱؾ͕औΕΔ (Լه͕ෳ߹Θͬͯ͞औಘͰ͖ Δ) w w פ͍ w
ཛྷ w շ w ಶΓ w Ӎ w ઇ w ໄ w ෩͕ڧ͍ -
Weather Awareness.SnapshotApi.getWeather(apiClient) .setResultCallback(new ResultCallback<WeatherResult>() { @Override public void onResult(@NonNull WeatherResult
weatherResult) { if (!weatherResult.getStatus().isSuccess()) { Log.e(TAG, "Could not get weather."); return; } Weather weather = weatherResult.getWeather(); showWeather(weather); } }); • SnapshotApi.getWeatherΛݺͿ͚ͩ • WeatherResultʹઌͷใ͕֨ೲ͞Ε͍ͯΔ -
どういうFenceを作ることができるのか • ԼهͷใΛऔಘͰ͖Δ w #FBDPO'FODF w %FUFDUFE"DUJWJUZ'FODF w )FBEQIPOF'FODF w
-PDBUJPO'FODF w 5JNF'FODF • ·ͨ͜ΕΒͷใΛΈ߹ΘͤΔ͜ͱՄೳ • ྫ͑ʮΓग़͔ͨ࣌ͭ͠ϔουϗϯϓϥά͕ϓϥάΠϯঢ়ଶͰ͋ Δʯͱ͍͏ϑΣϯεΛ࡞Δ͜ͱ͕Ͱ͖Δ -
Fenceを実際に作る AwarenessFence pluggingIn = HeadphoneFence.pluggingIn(); Awareness.FenceApi.updateFences(mClient, new FenceUpdateRequest.Builder() .addFence(FenceKey.PLUG_IN.name(), pluggingIn,
mFencePendingIntent).build()) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { if (status.isSuccess()) { Log.i(TAG, "fence start updateFences add fence: success"); } else { Log.i(TAG, "fence start updateFences add fence: fail" + status); } } }); • ϔουϗϯͷϓϥάΠϯঢ়ଶʹͳͬͨ࣌ͱ͍ ͏FenceΛ࡞Δ -
Fenceを実際に作る AwarenessFence pluggingIn = HeadphoneFence.pluggingIn(); Awareness.FenceApi.updateFences(mClient, new FenceUpdateRequest.Builder() .addFence(FenceKey.PLUG_IN.name(), pluggingIn,
mFencePendingIntent).build()) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { if (status.isSuccess()) { Log.i(TAG, "fence start updateFences add fence: success"); } else { Log.i(TAG, "fence start updateFences add fence: fail" + status); } } }); • Headphone FenceΛऔಘ -
Fenceを実際に作る AwarenessFence pluggingIn = HeadphoneFence.pluggingIn(); Awareness.FenceApi.updateFences(mClient, new FenceUpdateRequest.Builder() .addFence(FenceKey.PLUG_IN.name(), pluggingIn,
mFencePendingIntent).build()) .setResultCallback(new ResultCallback<Status>() { @Override public void onResult(@NonNull Status status) { if (status.isSuccess()) { Log.i(TAG, "fence start updateFences add fence: success"); } else { Log.i(TAG, "fence start updateFences add fence: fail" + status); } } }); • FenceAPI.updateFencesΛݺͿ • ͜Ε͚ͩ -
Fenceを登録する • ैདྷͷBroadcast Receiverͱಉ༷ public class FenceReceiver extends BroadcastReceiver {
private static final String TAG = "FENCE_RECEIVER"; @Override public void onReceive(Context context, Intent intent) { if (!TextUtils.equals("com.kgmyshin.fence.RECEIVE_FENCE", intent.getAction())) { return; } FenceState state = FenceState.extract(intent); if (TextUtils.equals(state.getFenceKey(), FenceKey.PLUG_IN.name())) { if (state.getCurrentState() == FenceState.TRUE) { Toast.makeText(context, "plug in", Toast.LENGTH_SHORT).show(); } Log.i(TAG, "FENCE_START: " + state.getCurrentState()); } } } <receiver android:name=".fence.FenceReceiver"> <intent-filter> <action android:name="com.kgmyshin.fence.RECEIVE_FENCE" /> </intent-filter> </receiver> -
注意点1 • Snapshotʹ͋ΔΑ͏ͳWeather ͱ Nearby PlacesͰFenceΛ࡞Δ͜ͱͰ͖ͳ͍ • ʮӍ͕߱ͬͨΒ…ʯɺʮༀہͷۙ͘ʹདྷͨ Β…ʯͱ͍͏ͷ͕Ͱ͖ͳ͍ •
ΔͳΒɺ࣌ؒڑͷมಈΛఆظతʹݟ ͯSnapshotͰऔಘ͢Ε͍͚ͦ͏