Java Android GeoFerning接收器未触发
我目前正在为android制作一个地理围栏应用程序,我遵循了他们的建议,但我似乎无法正确触发广播接收器 已调用onAddGeofencesResult,但从未调用过BroadcastReceiver。你知道为什么吗?我有它,所以如果一个人在地球围栏内超过5毫秒,以及离开或进入地球围栏,它应该发送广播。我根据办公室附近的坐标制作了地理围栏,但在穿过(或站在里面)时没有得到任何结果 ReceiveTransitionsBroadcastReceiver.java:Java Android GeoFerning接收器未触发,java,android,android-geofence,Java,Android,Android Geofence,我目前正在为android制作一个地理围栏应用程序,我遵循了他们的建议,但我似乎无法正确触发广播接收器 已调用onAddGeofencesResult,但从未调用过BroadcastReceiver。你知道为什么吗?我有它,所以如果一个人在地球围栏内超过5毫秒,以及离开或进入地球围栏,它应该发送广播。我根据办公室附近的坐标制作了地理围栏,但在穿过(或站在里面)时没有得到任何结果 ReceiveTransitionsBroadcastReceiver.java: import com.parse.
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>
谢谢你的检查,谢谢你的帮助
编辑:土工栅栏的半径为1000米。我开了大约一英里远的车,但什么也没开,所以我想这不是准确性的问题。请注意,设备未激活,因此我只使用wifi,但我连接到的每个地理围栏周围都有几十个wifi点。Google Play GeoFerning API上的一个重要注意事项。我发现地理围栏从未从GPS硬件智能地检索位置。GeoFence API将观察操作系统提供的最准确位置,或者如果没有最近的位置读数可用,则会导致通过Wifi/蜂窝网络计算位置。(这很糟糕,因为蜂窝网络不准确,而且wifi通常不可用)
因此,要从Geofence API中获得响应性或准确的结果,您必须设置Geofence,然后每隔一段时间轮询GPS硬件,甚至不对接收到的结果进行任何操作,以便向操作系统和Geofence API提供有价值的数据 这可能是你没有看到任何结果的核心原因。geofence事件将不会触发,直到操作系统对该操作有100%的信心。因此,如果一个位置读数的精度为500米(使用蜂窝式地理定位时这并非不可能),而你的围栏半径为50米,你将永远无法产生入口事件,而你必须在距离围栏点至少约550m的地方才能产生出口事件
TLDR;每隔一段时间对GPS硬件进行轮询,而不对结果进行任何处理,您将开始获得更精确的地理围栏。stealthwang是对的,您不能依赖地理围栏API,因为报告的位置有时非常偏离。您需要执行位置筛选以确保报告正常 另外,请注意,超时值只是一个提示。许多平台只是忽略了这个值,所以如果您的设计需要可靠的定期更新,那么您需要自己实现它
祝您的项目好运。在不严重影响电池寿命的情况下轮询GPS硬件的最佳方法是什么?“每隔一段时间轮询GPS硬件,甚至不做任何有结果的事情”。。。然后你的应用程序会烧掉电池,用户想知道为什么他们的手机在不使用的时候会很热。。。他们查看电池部分,发现你的应用程序是最严重的违规者,并被列为最高电池使用率。。。然后他们会给你一个1星级的评级并卸载你的应用程序。所以是的,问题解决了。。。我猜。我的应用程序部署在这样一个环境中:由于设备是插入式的,所以准确性是至关重要的,功耗是微不足道的。是的,我的问题解决了。这不是我的错,除非你这么做,否则安卓GPS的精确度是无法忍受的。您必须针对自己的约束条件进行开发。如果你真的致力于精确性和电池寿命,那么增加你的间隔时间,并在间隔时间中实施一些退避逻辑。你曾经正确地工作过吗?
@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);
}