我目前正在为 android 制作一个地理围栏应用程序,我按照他们的教程进行操作,但我似乎无法让 BroadcastReceiver 正确触发。
onAddGeofencesResult 被调用,但 BroadcastReceiver 从未被调用。为什么?我有它,所以如果一个人在地理围栏中超过 5 毫秒,以及退出或进入地理围栏,它应该发送广播。我根据我办公室附近的坐标制作了地理围栏,但在穿过它们(或站在其中)时没有得到任何结果。
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();
}
}
显现:
<?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:
@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);
}
感谢您查看此内容,感谢您的帮助。
编辑:地理围栏的半径为 1000 米。我开车大约一英里远,但什么也没有,所以我认为这不是问题的准确性。请注意,设备未激活,所以我只使用 wifi,但每个地理围栏周围有几十个 wifi 点,我连接到这些点。