- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用来自 google play 服务的地理围栏 api,但我无法让我的广播接收器触发它的 onReceive()
方法。
看起来我尊重让它工作所需的一切,但我没有得到任何过渡事件。
ACCESS_FINE_LOCATION
权限BroadcastReceiver
并将导出的值设置为“true”(有人说这是必需的)ACCESS_MOCK_LOCATION
权限)但是,我仍然获得了成功的 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/
我的应用程序无法在 Android 7 上运行。调用了我的 BroadcastReceiver.onReceive 方法,但缺少 intent.getExtras 的内容。我已验证数据已正确加载。这是
我认为即使我退出应用程序,BroadcastReceiver AlarmManagerBroadcastReceiver 也会继续运行。我认为 BroadcastReceiver 会在我重新启动系统时
我为 Wi-Fi 创建了一个 brodcastReceiver,一切正常,除了,当我午餐应用程序时,broadcastreceiver 开始直接扫描,我无法停止它,即使我离开应用。我试图从主要 Act
我最近开始在我的 Android 应用程序上使用 Google Analytics(分析)。我想知道如何在 BroadcastReceiver 的 onReceive 方法中将事件发送到 GA。这是一
我想在 Broadcast Receiver 中检查互联网连接;并将结果( bool 标志)设置为全局变量,以便在 if 条件下将其用于整个应用程序;如果互联网断开连接,则在主要 Activity 中
如果用户向上或向下按下音量键,是否可以在我的广播接收器中检测到它?我需要完整的代码。 这是我的 Intent 过滤器 IntentFilter filter = new IntentFilter();
我正在尝试编写一个应用程序,其中我根据使用 gcm 推送通知发送的消息对 UI 进行更改,并且我设法通过使用 BroadcastReceiver onReceive 函数来实现它来实现它,但它仅在应用
我正在尝试使用 AlarmManager 安排通知当我安排一个通知时它工作得很好,但是当我安排两个通知时,第一个通知没问题,但第二个通知不起作用。 我发现几分钟后打开应用程序会通知第二个通知。我认为我
我正在阅读指南 here关于设置 BroadcastReceiver 来检查电池变化。它指出我可以像这样设置一个 BroadcastReceiver:
我想在应用程序中接收有关启用/禁用位置提供程序的信息,例如NETWORK、GPS等。我创建了简单的BroadcastReceiver: public class TestReciver extends
我正在使用广播接收器来设置警报警报,但由于某种原因它无法工作。 public class SimpleSleepActivity extends Activity { /** Called when
当我的应用程序处于后台时,我尝试播放一些音调,并按下“相机”按钮,因为我正在执行此简单的步骤。 创建BroadcastReceiver类 public class CameraButtonListen
我创建了我的广播接收器 AnprEventReciever,它应该在连接状态发生变化时被触发,但它并没有。 事件接收者: import android.content.BroadcastReceive
我有一个时间选择器来设置闹钟 public class TimePickerFragment : DialogFragment, TimePickerDialog.IOnTimeSetListener
我目前正在开发一个使用 broadcastreceiver 来检查收到的短信的应用程序,但突然间它似乎停止工作了,我什至写了一个小的测试应用程序,以至少对我来说,语法上似乎是正确的,但也不起作用。 这
我正在尝试使用 BroadcastReceiver 获取互联网连接状态。 接收类是这样的: public class ConnectivityReceiver extends BroadcastRec
我有一个小部件,它只使用服务来更新小部件的 RemoteView 的外观。该服务基于由不同注册的 BroadcastReceiver 提供的过滤器启动。当满足其中一个条件时,将调用我的 Broadca
我没有时间玩接收器,而且我对2.3.x以上的东西都不熟悉,所以当我读到这个问题时,我感到非常困惑: Can BroadcastReceiver registered in AndroidManifes
我有一个录音机 android 线程,我需要知道录音时麦克风/耳机是否已连接,所以我需要在线程内使用 BroadcastReceiver()。我如何注册它? this.registerReceiver
大家好,感谢您的帮助!我将从代码开始 所以,带有广播接收器的类是这样的: public class MyService extends Service { // ... // ACTI
我是一名优秀的程序员,十分优秀!