gpt4 book ai didi

未触发 Android 地理围栏 API BroadcastReceiver

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:06:59 31 4
gpt4 key购买 nike

我正在尝试使用来自 google play 服务的地理围栏 api,但我无法让我的广播接收器触发它的 onReceive() 方法。

看起来我尊重让它工作所需的一切,但我没有得到任何过渡事件。

  • 我在我的依赖项中包含了 google play 服务
  • 我请求ACCESS_FINE_LOCATION权限
  • 我在我的 list 中注册了我的 BroadcastReceiver 并将导出的值设置为“true”(有人说这是必需的)
  • 我在我所在的确切位置添加了一个大型地理围栏(我使用手机上的 Google map 对其进行了验证),具有两种过渡类型且没有过期时间
  • 我尝试在地理围栏中间使用一个精确的模拟位置,并在 10 秒后在地理围栏之外尝试另一个位置(并且我确保存在 ACCESS_MOCK_LOCATION 权限)
  • 我已确认我的手机已开启定位功能并且其模式设置为高精度
  • 我验证了互联网连接
  • 我尝试在添加地理围栏后断开和不断开 LocationClient

但是,我仍然获得了成功的 onAddGeofencesResult() 回调,但没有 onReceive() 回调。我在 Nexus 5 和 Nexus 7 上尝试过,但都未能触发地理围栏转换。

我还在 https://developer.android.com/training/location/geofencing.html 上尝试了示例, 但其行为是相同的。

这是我的 BroadcastReceiver 类:

public class GeofencesReceiver extends BroadcastReceiver implements ConnectionCallbacks, OnConnectionFailedListener, OnAddGeofencesResultListener {
private final static String TAG = "GeofencesReceiver";

private Context context = null;
private LocationClient locationClient = null;

/**
* An empty constructor is needed by the manifest.
*/
@SuppressWarnings("unused")
public GeofencesReceiver(){}

public GeofencesReceiver(Context context){
this.context = context;
locationClient = new LocationClient(context, this, this);
locationClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
new Thread(new Runnable() {
@Override
public void run() {
List<Geofence> geofences = getGeofences();
if(geofences.size() > 0){
Intent geofenceBroadcastReceiverIntent = new Intent(context, GeofencesReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceBroadcastReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT);
locationClient.addGeofences(geofences, pi, GeofencesReceiver.this);
}else
Log.w(TAG, "No GPS zone found");
}
}).start();
}

private List<Geofence> getGeofences(){
Vector<Geofence> geofences = new Vector<Geofence>();
Geofence geofence = new Geofence.Builder()
.setRequestId("1")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(45.2676018, -72.1572185, 100)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
geofences.add(geofence);
return geofences;
}

@Override
public void onDisconnected() {
Log.d(TAG, "onDisconnected");
}

@Override
public void onAddGeofencesResult(int i, String[] strings) {
if(i != LocationStatusCodes.SUCCESS)
Log.e(TAG, "Failed to add geofences");
else
Log.d(TAG, "Geofences successfully added");
locationClient.disconnect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG, "onConnectionFailed");
}

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
if(LocationClient.hasError(intent)){
int errorCode = LocationClient.getErrorCode(intent);
Log.e(TAG, "Location Services error: " + Integer.toString(errorCode));
return;
}
int transitionType = LocationClient.getGeofenceTransition(intent);
if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
for(Geofence geofence : triggerList){
Log.d(TAG, (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ? "Entering" : "Exiting") + " GPS zone " + geofence.getRequestId());
}
}else{
Log.e(TAG, "Geofence transition error: " + transitionType);
}
}
}

在我的 list 中,我有以下几行(不在那个结构中)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<receiver android:name="novom.anyware.anywaresdk.GeofencesReceiver" android:exported="true" />
<meta-data android:name="com.google.android.gms.version" android:value="4323000" />

在我的 build.gradle 文件中,我包含了这样的服务

dependencies {
compile 'com.google.android.gms:play-services:4.3.23'
}

我花了一整天试图弄清楚为什么它不起作用......我从 Logcat 找到了这些行,但我不确定它们是否与问题有关,因为我在 Google 上找不到任何帖子关于地理围栏的那些错误。

04-07 09:01:14.631    1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Not calling LocationReportingService, hasMoved: true, elapsed millis: 580166, request: Phone

04-07 09:02:16.481 1160-1319/? I/GCoreUlr﹕ Successfully inserted location
04-07 09:02:16.501 1160-1319/? I/GCoreUlr﹕ Starting service, intent=Intent { cmp=com.google.android.gms/com.google.android.location.reporting.LocationReportingService }, extras=null
04-07 09:02:16.571 1210-1252/? W/GLSUser﹕ GoogleAccountDataService.getToken()
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Failed write_ctrl(u 69) res=-1 errno=22
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Untagging socket 69 failed errno=-22
04-07 09:02:16.601 1160-3780/? W/NetworkManagementSocketTagger﹕ untagSocket(69) failed with errno -22
04-07 09:02:16.601 1160-3780/? I/imp﹕ I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond
04-07 09:02:16.601 1160-3780/? I/imp﹕ Retrying request
04-07 09:02:17.501 1160-6156/? I/GCoreUlr﹕ Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_ACTIVE_STATE cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService }, extras=null
04-07 09:02:17.511 1160-6156/? I/GCoreUlr﹕ Batch Location Update succeeded for account account#7#
04-07 09:02:17.571 1160-1319/? I/GCoreUlr﹕ Ensuring that reporting is active for [account#7#]

如果没有人能帮助我,我担心我会使用向 GPS_PROVIDER 注册的 LocationManager 实现我自己的地理围栏算法,这会耗尽用户的电池...

最佳答案

首先,你确定你的坐标是正确的吗?我在玩地理围栏时遇到了一个非常相似的问题,我花了一段时间才意识到我的坐标只是倒转了。

不用说,我觉得自己很愚蠢。

关于未触发 Android 地理围栏 API BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22872794/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com