awareness api

awareness api

3c4e24fd827c789cb67a9f759f057b06?s=128

Shinnosuke Kugimiya

July 22, 2016
Tweet

Transcript

  1. Awareness API Retty Tech Cafe #6 / @kgmyshin

  2. agenda • Awareness APIͱ͸ͳʹ͔ • Awareness APIΛ࢖͏ • ಋೖ •

    Snapshot API • Fence API • ॴײ
  3. Awareness APIͱ͸Կ͔

  4. Awareness APIͱ͸Կ͔ • ༷ʑͳίϯςΩετΛऔಘͰ͖ΔAPI ը૾ࢀর: https://developers.google.com/awareness/

  5. Awareness API 4OBQTIPU"1* 'FODF"1* ࣗ෼͕ཉ͍͠͞·͟·ͳঢ়ଶ Λऔಘ͢Δ΋ͷ ࣗ෼͕ཉ͍͠ঢ়ଶΛఆٛͯ͠ɺ ͦͷঢ়ଶʹͳΔͱBroadcast͕ ඈΜͰ͘Δͱ͍͏΋ͷ

  6. Awareness APIΛ࢖͏

  7. ಋೖ • ैདྷͷGoogle Play ServiceܥͷAPIಋೖํ๏ͱಉ༷ 1. Google Developers ConsoleͰAPIΛ༗ޮʹͯ͠ɺAPI Keyऔಘ

    2. build.gradleͷdependenciesʹ௥Ճ 3. AndroidManifest.xmlʹmeta-dataΛهೖ 4. AndroidManifest.xmlʹඞཁͳPermissionΛ௥Ճ
  8. [ݹ͍] 9.2.0 Google Play Services • AnalyticsͷΑ͏ʹɺSDK͕෼ׂ͞Ε͍ͯͳ͍ ͷͰؙͬͱೖΕͳ͍ͱ͍͚ͳ͍ɻ DPNHPPHMFBOESPJEHNTQMBZTFSWJDFTBOBMZUJDT DPNHPPHMFBOESPJEHNTQMBZTFSWJDFTBXBSFOFTT

    ͳ͍
  9. ͪͳΈʹ… • Google Play ServicesΛؙͬͱೖΕͯɺ Support Library΋ೖΕΔͱMethod Count͕ 655536Λ௒͑Δ orz

    • ೋ͚ͭͩͳͷʹMultiDexͳͲ͕ඞཁorz
  10. 9.2.1 Google Play Services 9.2.1Ͱ͸awareness api༻ͷ΋ͷ͕Ͱ͖͍ͯͨɻ DPNHPPHMFBOESPJEHNTQMBZTFSWJDFTDPOUFYUNBOBHFS awarenessͰ͸ͳ͍… ϝιουΧ΢ϯτΛଌΔͱ10252ɻ Google

    Play Service AllͰ͸4ສ͘Β͍ͳͷͰ3ສ͘Β͍࡟ΕͯΔɻ
  11. Snapshot API

  12. Current Activity • ݱࡏͷϢʔβʔͷঢ়ଶ͕औΕΔ w ࣗಈंͱ͔ʹ৐ͬͯΔ w ࣗసंʹ৐ͬͯΔ w า͍ͯΔ͔૸ͬͯΔ

    w ૸ͬͯΔ w ࢭ·ͬͯΔ w ܏͍ͯΔ w า͍ͯΔ
  13. 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ʹઌͷ৘ใ͕֨ೲ͞Ε͍ͯΔ
  14. Nearby Beacons • ۙ͘ͷBeaconͷ৘ใΛऔಘͰ͖Δ • ͜Ε͸ࢼͯ͠ͳ͍ orz

  15. Headphone State • ϔουϗϯϓϥάͷঢ়ଶ͕औΕΔ w ϓϥάΠϯঢ়ଶ w Ξϯϓϥάঢ়ଶ

  16. 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ʹઌͷ৘ใ͕֨ೲ͞Ε͍ͯΔ
  17. Location • Ң౓ܦ౓͕औΕΔ

  18. 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ʹઌͷ৘ใ͕֨ೲ͞Ε͍ͯΔ
  19. Nearby Places • ۙ͘ͷࢪઃͷ৘ใ͕औΕΔ w ۙ͘ʹ͋Δࢪઃͷ w ໊લ w ి࿩൪߸

    w Ң౓ܦ౓ w XFCTJUFVSM w SBUJOH w Ձ֨Ϩϕϧ w ͳͲ Awareness APIҎ֎ʹ ผ్ Google Places APIΛ ༗ޮʹ͢Δඞཁ͕͋Δ ͷͰ஫ҙɻ
  20. 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ʹઌͷ৘ใ͕֨ೲ͞Ε͍ͯΔ
  21. Weather • ఱؾ͕औΕΔ (Լه͕ෳ਺߹Θͬͯ͞औಘͰ͖ Δ) w ΋΍ w פ͍ w

    ཛྷ w շ੖ w ಶΓ w Ӎ w ઇ w ໄ w ෩͕ڧ͍
  22. 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ʹઌͷ৘ใ͕֨ೲ͞Ε͍ͯΔ
  23. Fence API

  24. վΊͯFence APIͱ͸ • Fence API͸ࣗ෼͕ཉ͍͠ঢ়ଶΛఆٛͯ͠ɺͦ ͷঢ়ଶʹͳΔͱBroadcast͕ඈΜͰ͘Δͱ͍͏ ΋ͷ

  25. どういうFenceを作ることができるのか • Լهͷ৘ใΛऔಘͰ͖Δ w #FBDPO'FODF w %FUFDUFE"DUJWJUZ'FODF w )FBEQIPOF'FODF w

    -PDBUJPO'FODF w 5JNF'FODF • ·ͨ͜ΕΒͷ৘ใΛ૊Έ߹ΘͤΔ͜ͱ΋Մೳ • ྫ͑͹ʮ૸Γग़͔ͨ࣌ͭ͠ϔουϗϯϓϥά͕ϓϥάΠϯঢ়ଶͰ͋ Δʯͱ͍͏ϑΣϯεΛ࡞Δ͜ͱ͕Ͱ͖Δ
  26. 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Λ࡞Δ
  27. 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Λऔಘ
  28. 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ΛݺͿ • ͜Ε͚ͩ
  29. 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>
  30. 注意点1 • Snapshotʹ͋ΔΑ͏ͳWeather ͱ Nearby PlacesͰ͸FenceΛ࡞Δ͜ͱ͸Ͱ͖ͳ͍ • ʮӍ͕߱ͬͨΒ…ʯɺʮༀہͷۙ͘ʹདྷͨ Β…ʯͱ͍͏ͷ͕௚઀͸Ͱ͖ͳ͍ •

    ΍ΔͳΒɺ࣌ؒ΍ڑ཭ͷมಈΛఆظతʹݟ ͯSnapshotͰऔಘ͢Ε͹͍͚ͦ͏
  31. 注意点2 • ຊମͷిݯ͕੾ΕΔͱFence͕ͳ͘ͳΔΈ͍ͨ • BOOT_COMPLETEΛड৴ͯ͠࠶ొ࿥ͷඞཁ ͕͋Δ

  32. ॴײ • ಋೖίετ͘͢͝௿ͯ͘࢖͍қ͍ • ᠘΋গͳ͍ҹ৅ • ಛʹPlaces͸໘നͦ͏ • ΞΠσΞ࣍ୈͰ͘͢͝͸·Γͦ͏