- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经使用 GoogleApiClient 实现了地理围栏 -> 当触发时,一个服务连接到 GoogleApiClient 并添加几个地理围栏。
在我将另一个 IntentService 注册为地理围栏事件的“回调”之前。这或多或少起作用,但仅当应用程序处于前台时。因为我也想在应用程序处于后台/关闭时获取地理围栏事件,所以我进行了一些搜索并将回调从 IntentService 移动到 BroadcastReceiver,现在当应用程序处于前台时我什至没有获取地理围栏事件。
我已经在互联网上寻求解决方案(最常见的答案:将事件监听器从 Service 更改为 BroadCastReceiver - 但这让事情变得更糟)
这是我的设置:
list :
<receiver
android:name=".service.GeofenceReceiver">
<intent-filter>
<action android:name="com.example.geofence.ACTION_RECEIVE" />
</intent-filter>
</receiver>
<receiver android:name=".service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".service.GeofenceRegistrationService"
android:enabled="true"
android:exported="false" />
启动接收器:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
GeofenceRegistrationService.startService(context,true);
}
}
地理围栏接收器:
public class GeofenceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Logger.e(this, "GeofenceReceiver called");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
Logger.e(this, String.valueOf(geofencingEvent.getErrorCode()));
} else {
Logger.i(this, geofencingEvent.getTriggeringLocation().getLatitude() + ", " +geofencingEvent.getTriggeringLocation().getLongitude());
}
}
}
地理围栏注册服务:
public class GeofenceRegistrationService extends Service implements ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status> {
public static final String KEY_FORCE_CLEAN = "FORCE_CLEAN";
public static void startService(Context context, boolean forceClean) {
Intent intent = new Intent(context, GeofenceRegistrationService.class);
intent.putExtra(KEY_FORCE_CLEAN, forceClean);
context.startService(intent);
}
private GoogleApiClient mGoogleApiClient;
private PendingIntent mGeofencePendingIntent;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (isLocationPermissionGranted() && intent != null) {
startGeofenceRegistration();
} else {
this.stopSelf(startId);
}
return START_REDELIVER_INTENT;
}
public void startGeofenceRegistration() {
if (mGoogleApiClient == null) {
mGoogleApiClient = buildAPIClient();
}
if (!mGoogleApiClient.isConnected() && !mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
} else {
registerGeofences();
}
}
private boolean isLocationPermissionGranted() {
return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
private synchronized GoogleApiClient buildAPIClient() {
return new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private void addGeoFences() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
try {
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
createGeofencesRequest(),
getGeofencePendingIntent()
).setResultCallback(this);
} catch (SecurityException securityException) {
Logger.e(this, securityException);
}
}
}
private void removeGeoFences() {
if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
try {
LocationServices.GeofencingApi.removeGeofences(
mGoogleApiClient,
getGeofencePendingIntent()
).setResultCallback(this);
} catch (SecurityException securityException) {
Logger.e(this, securityException);
}
}
}
private PendingIntent getGeofencePendingIntent() {
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
//Intent intent = new Intent(this, GeofenceEventHandlingService.class);
Intent intent = new Intent("com.example.geofence.ACTION_RECEIVE");
mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Logger.d(this, "CONNECTION_FAILED");
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Logger.d(this, "CONNECTED");
registerGeofences();
}
private void registerGeofences() {
removeGeoFences();
addGeoFences();
}
@Override
public void onConnectionSuspended(int i) {
Logger.d(this, "connection suspended");
}
private GeofencingRequest createGeofencesRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
List<Poi> pois = Config.getPersistenceService().getPois();
int counter = 0;
for (Poi p : pois) {
if (p.isCoordinateSet()) {
Geofence fence = new Geofence.Builder()
.setRequestId(String.valueOf(p.getId()))
.setCircularRegion(p.getLat(), p.getLon(), 2500) // TODO
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.build();
builder.addGeofence(fence);
counter++;
}
}
Logger.i(this, "added geofences: " + counter);
return builder.build();
}
@Override
public void onDestroy() {
if (mGoogleApiClient != null && (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())) {
mGoogleApiClient.disconnect();
}
super.onDestroy();
}
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Logger.d(this, "Geofences added");
} else {
Logger.d(this, "Failed to add geofences");
}
}
}
我做错了什么?我监督什么?
最佳答案
这是我已经完成的工作,但请记住,我是在 Activity 中添加和删除我的地理围栏,并在 IntentService 中接收地理围栏转换。
我认为您应该尝试构建您的 Intent
指定 new Intent(context, YourReceiverClass)
并使用 PendingIntent 创建您的
。这是我使用的:PendingIntent
。 getBroadcast()
private PendingIntent getGeofencePendingIntent(){
// Re-use the Pending Intent if it already exists
if(mGeofencePendingIntent != null){
return mGeofencePendingIntent;
}
// The intent for the IntentService to receive the transitions
Intent intent = new Intent(getContext(), GeofenceTransitionsIntentService.class);
// Create the pending intent
mGeofencePendingIntent = PendingIntent
.getService(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
在我的 IntentService 中:
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceErrorMessages.getErrorString(this,
geofencingEvent.getErrorCode());
Log.e(LOG_TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Do something with geofences
}
}
关于Android 地理围栏 BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42414838/
我的应用程序无法在 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
我是一名优秀的程序员,十分优秀!