Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. compile 'com.google.android.gms:play-services-location:+'
  2.  
  3. compile 'com.google.android.gms:play-services-contextmanager:+'
  4.  
  5. <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
  6. <uses-permission android:name="android.permission.INTERNET" />
  7. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  8.  
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@mipmap/ic_launcher"
  12. android:label="@string/app_name"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme" >
  15.  
  16. <meta-data
  17. android:name="com.google.android.awareness.API_KEY"
  18. android:value="PUT_YOUR_AWARENESS_KEY_HERE" />
  19.  
  20. <activity android:name=".MainActivity" >
  21. <intent-filter>
  22. <action android:name="android.intent.action.MAIN" />
  23.  
  24. <category android:name="android.intent.category.LAUNCHER" />
  25. </intent-filter>
  26. </activity>
  27. </application>
  28.  
  29. public class MainActivity extends Activity {
  30.  
  31. private GoogleApiClient mGoogleApiClient;
  32. private PendingIntent mPendingIntent;
  33. private FenceReceiver mFenceReceiver;
  34.  
  35. // The intent action which will be fired when your fence is triggered.
  36. private final String FENCE_RECEIVER_ACTION = BuildConfig.APPLICATION_ID + "FENCE_RECEIVER_ACTION";
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Awareness.API).build();
  43. mGoogleApiClient.connect();
  44. // Set up the PendingIntent that will be fired when the fence is triggered.
  45. mPendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(FENCE_RECEIVER_ACTION), 0);
  46. // The broadcast receiver that will receive intents when a fence is triggered.
  47. mFenceReceiver = new FenceReceiver();
  48. registerReceiver(mFenceReceiver, new IntentFilter(FENCE_RECEIVER_ACTION));
  49. createFence(DetectedActivityFence.IN_VEHICLE, "InVehicleFence");
  50. }
  51.  
  52. @Override
  53. public void onDestroy() {
  54. try {
  55. unregisterReceiver(mFenceReceiver); //Don't forget to unregister the receiver
  56. } catch (Exception e) {
  57. e.printStackTrace();
  58. }
  59. super.onDestroy();
  60. }
  61.  
  62. private void createFence(int detectedActivityFence, final String fenceKey) {
  63. AwarenessFence fence = DetectedActivityFence.during(detectedActivityFence);
  64. // Register the fence to receive callbacks.
  65. Awareness.FenceApi.updateFences(
  66. mGoogleApiClient, new FenceUpdateRequest.Builder().addFence(fenceKey, fence, mPendingIntent)
  67. .build()).setResultCallback(new ResultCallback<Status>() {
  68. @Override
  69. public void onResult(@NonNull Status status) {
  70. if (status.isSuccess()) {
  71. Log.i(getClass().getSimpleName(), "Successfully registered.");
  72. } else {
  73. Log.e(getClass().getSimpleName(), "Could not be registered: " + status);
  74. }
  75. }
  76. });
  77. }
  78.  
  79. // Handle the callback on the Intent.
  80. public class FenceReceiver extends BroadcastReceiver {
  81. @Override
  82. public void onReceive(Context context, Intent intent) {
  83. FenceState fenceState = FenceState.extract(intent);
  84. switch (fenceState.getCurrentState()) {
  85. case FenceState.TRUE:
  86. Log.i(fenceState.getFenceKey(), "Driving");
  87. break;
  88. case FenceState.FALSE:
  89. Log.i(fenceState.getFenceKey(), "Not driving");
  90. break;
  91. }
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement