gpt4 book ai didi

Android Geofence 无法调用 IntentService 并且无法在 BroadcastReceiver 中工作

转载 作者:太空狗 更新时间:2023-10-29 16:03:38 26 4
gpt4 key购买 nike

当我尝试在我的应用程序中使用地理围栏时遇到问题。我已成功注册地理围栏,但我从未收到通知。

首先,我按照 http://developer.android.com/training/location/geofencing.html 中提供的示例进行操作,但即使我走向或远离区域,代码也不会触发 Intent 服务。

我的注册地理围栏和调用 Intent 服务的代码如下:

内部 list :

<service
android:name="com.example.zukami.apps.blynk.geofence.ReceiveTransitionsIntentService"
android:exported="true" >
</service>

在我的 MainActivity 中

    private boolean registerGeofence() {
mRequestType = GeofenceUtils.REQUEST_TYPE.ADD;
if (!servicesConnected()) {
return false;
}


SimpleGeofence testGeo = new SimpleGeofence(Flag.GEOFENCE + "_"
+ 1, Double.valueOf("1.317342"),
Double.valueOf("103.841998"), (float) (200),
GEOFENCE_EXPIRATION_IN_HOURS,
Geofence.GEOFENCE_TRANSITION_ENTER);

mPrefs.setGeofence(Flag.GEOFENCE + "_" + 1, testGeo);
mCurrentGeofences.add(testGeo.toGeofence());

SimpleGeofence testGeo2 = new SimpleGeofence(Flag.GEOFENCE + "_"
+ 2, Double.valueOf("1.303961"),
Double.valueOf("103.909356"), (float) (200),
GEOFENCE_EXPIRATION_IN_HOURS,
Geofence.GEOFENCE_TRANSITION_ENTER);

mPrefs.setGeofence(Flag.GEOFENCE + "_" + 2, testGeo2);
mCurrentGeofences.add(testGeo2.toGeofence());
// end deleted this code after testing

try {
mGeofenceRequester.addGeofences(mCurrentGeofences);
Log.e(TAG, "ADDING GEOFENCE ??? YES WE DID IT");
} catch (UnsupportedOperationException e) {
Toast.makeText(this,
R.string.add_geofences_already_requested_error,
Toast.LENGTH_LONG).show();
}
return true;
}

@Override
protected void onResume() {
super.onResume();
// Register the broadcast receiver to receive status updates
LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver, mIntentFilter);

在我的地理围栏请求器中

private PendingIntent createRequestPendingIntent() {
Log.e(TAG, "CREATE REQUEST PENDING INTENT");
// If the PendingIntent already exists
if (null != mGeofencePendingIntent) {
Log.e(TAG, "PENDING INTENT NOT NULL");
return mGeofencePendingIntent;

// If no PendingIntent exists
} else {
Log.e(TAG, "PENDING INTENT NULL, LET'S CREATED IT");
// Create an Intent pointing to the IntentService
Intent intent = new Intent(mActivity,
ReceiveTransitionsIntentService.class);
Log.e(TAG,
return PendingIntent.getService(mActivity, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}

并且在类中,IntentService 的子类与示例保持相同。

我搜索了这个问题并找到了这个链接描述的解决方案: Android Geofence eventually stop getting transition intents ,所以我完全按照建议更改了我的代码,但我仍然无法从我在 GeofenceReceiver 类中的代码,这一行中的代码中获得通知

LocationClient.getGeofenceTransition(intent);

总是返回我-1,这意味着我永远不会进入或离开该区域(指示 GEOFENCE NEVER_EXPIRED),所以我永远不会收到通知。请在下面找到我的 GeofenceReceiver 类:

public class GeofenceReceiver extends BroadcastReceiver {

public static String TAG = GeofenceReceiver.class.getCanonicalName();
Context context;
Intent broadcastIntent = new Intent();

@Override
public void onReceive(Context context, Intent intent) {
this.context = context;

broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

if (LocationClient.hasError(intent)) {
handleError(intent);
} else {
handleEnterExit(intent);
}
}

private void handleError(Intent intent) {
int errorCode = LocationClient.getErrorCode(intent);
String errorMessage = LocationServiceErrorMessages.getErrorString(
context, errorCode);
Log.e(GeofenceUtils.APPTAG, context.getString(
R.string.geofence_transition_error_detail, errorMessage));

broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);
LocalBroadcastManager.getInstance(context).sendBroadcast(
broadcastIntent);
}

private void handleEnterExit(Intent intent) {

int transition = LocationClient.getGeofenceTransition(intent);

// Test that a valid transition was reported
if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
|| (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {



// Post a notification
List<Geofence> geofences = LocationClient
.getTriggeringGeofences(intent);
String[] geofenceIds = new String[geofences.size()];
String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,
geofenceIds);
String transitionType = getTransitionString(transition);

for (int index = 0; index < geofences.size(); index++) {
Geofence geofence = geofences.get(index);
geofenceIds[index] = geofences.get(index).getRequestId();
}

sendNotification(transitionType, ids);

// Create an Intent to broadcast to the app
broadcastIntent
.setAction(GeofenceUtils.ACTION_GEOFENCE_TRANSITION)
.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_ID, geofenceIds)
.putExtra(GeofenceUtils.EXTRA_GEOFENCE_TRANSITION_TYPE,
transitionType);

LocalBroadcastManager.getInstance(context).sendBroadcast(
broadcastIntent);

// Log the transition type and a message
Log.e(GeofenceUtils.APPTAG, transitionType + ": " + ids);
Log.e(GeofenceUtils.APPTAG, context
.getString(R.string.geofence_transition_notification_text));
Log.e(GeofenceUtils.APPTAG, "transition");
} else {
// Always log as an error
Log.e(TAG, "TRANSITION = " + transition);
}
}

请告知我应该如何修复此代码,非常感谢您提供的任何帮助。

最佳答案

对我来说,我只是更改了createPendingIntent方法中的一些代码行

private PendingIntent createRequestPendingIntent() {
Log.e(TAG, "CREATE REQUEST PENDING INTENT");
// If the PendingIntent already exists
if (null != mGeofencePendingIntent) {
Log.e(TAG, "PENDING INTENT NOT NULL");
return mGeofencePendingIntent;

// If no PendingIntent exists
} else {
Log.e(TAG, "PENDING INTENT NULL, LET'S CREATED IT");
// Create an Intent pointing to the IntentService
Intent intent = new Intent(mActivity,
ReceiveTransitionsIntentService.class);
Log.e(TAG,
return PendingIntent.getService(mActivity, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}

所以代替 返回 PendingIntent.getService(mActivity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

用这 block 蛋糕返回 PendingIntent.getBroadcast(mActivity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

并确保您已在应用程序 list 文件中声明了您的 BroadcastReceiver。祝您好运!!!!

关于Android Geofence 无法调用 IntentService 并且无法在 BroadcastReceiver 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22548706/

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