I am currently making a Geofencing app for android, and I followed their tutorial but I cant seem to get the BroadcastReceiver to trigger correctly.
The onAddGeofencesResult is called, but the BroadcastReceiver is not ever called. And idea why? I have it so that it should be sending a broadcast if a person is in the geofence for more than 5 ms, as well as exits or enters the geofence. I made geofences based on coordinates right near my office, but don't get any result when walking through them (or standing in them).
ReceiveTransitionsBroadcastReceiver.java:
import com.parse.ParseObject;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* This class receives geofence transition events from Location Services, in the
* form of an Intent containing the transition type and geofence id(s) that triggered
* the event.
*/
public class ReceiveTransitionsBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
System.out.println("Hello");
ParseObject gameScore = new ParseObject("EventTest");
gameScore.put("Student", "Josh");
gameScore.put("Entered", true);
gameScore.put("Class", "AndroidTest");
gameScore.saveInBackground();
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.spotter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.spotter.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.spotter.ReceiveTransitionsBroadcastReceiver" android:exported="false"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
MainActivity where I set up the geofences:
@Override
public void onConnected(Bundle bundle) {
System.out.println("Connected to LocationServices");
Geofence.Builder builder = new Geofence.Builder();
builder.setCircularRegion(41.909858, -87.649038, 100.0f)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setRequestId("1")
.setLoiteringDelay(5)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT | Geofence.GEOFENCE_TRANSITION_DWELL);
Geofence fence1 = builder.build();
builder.setCircularRegion(41.910359, -87.651444, 100.0f)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setRequestId("2")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT);
Geofence fence2 = builder.build();
Intent intent = new Intent(this, ReceiveTransitionsBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.addGeofences(Arrays.asList(fence1,fence2), pi, this);
}
Thanks for checking this out, I appreciate the help.
EDIT: The radius of the geofence is 1000 meters. I drive about a mile away and back but nothing, so I assume it's not the accuracy that's an issue. Note that the device is not activated, so I am only using wifi, but there are dozens of wifi spots around each geofence, which I connect to.