Android Geofence禁用重新启动位置服务时自动删除

Android Geofence禁用重新启动位置服务时自动删除,第1张

概述我已经使用FlagNEVER_EXPIRE成功添加了地理围栏.一切似乎都运转正常.但现在在测试时我发现如果我停止位置服务地理围栏停止工作为EXPECTED.此外,当我再次启动位置服务时,我之前添加的地理围栏应该再次开始工作,但不会生成任何通知,并且一旦禁用位置服务,地理围栏似乎会自动删除.

我已经使用Flag NEVER_EXPIRE成功添加了地理围栏.一切似乎都运转正常.

但现在在测试时我发现如果我停止位置服务地理围栏停止工作为EXPECTED.此外,当我再次启动位置服务时,我之前添加的地理围栏应该再次开始工作,但不会生成任何通知,并且一旦禁用位置服务,地理围栏似乎会自动删除.我必须再次设置所有位置以恢复工作状态下的地理围栏.

任何建议或任何想法为什么它这样做?

编辑::

当设备关闭/重新启动等时也会出现类似的问题.因此,如果禁用位置服务/重新启动设备,基本上所有已注册的地理围栏都将过期.其中很少我尝试通过Session处理,但我正在寻找一种解决方案,通过该解决方案,我们可以在启用位置服务时将Geofences设置回来.

解决方法:

为了在后台观看,我遇到了同样的问题,并且能够通过将示例代码从IntentService更改为broadcastReceiver来解决它.所有细节都可以在我的帖子中找到:

Android Geofence eventually stop getting transition intents

这就是我所说的(如果有人懒得跟随链接):

因此,在稍微讨论一下后,看起来示例代码中定义的ReceiveTransitionsIntentService将在应用程序不在时停止获取通知.我认为这是示例代码的一个大问题……似乎这样会让像我一样的人绊倒.

所以我使用了广播接收器,到目前为止它似乎正在我的测试中工作.

将其添加到清单:

<receiver androID:name="com.aol.androID.geofence.GeofenceReceiver"        androID:exported="false">        <intent-filter >            <action androID:name="com.aol.androID.geofence.ACTION_RECEIVE_GEOFENCE"/>        </intent-filter>    </receiver>

然后在GeofenceRequester类中,您需要更改createRequestPendingIntent方法,以便它转到broadcastReceiver而不是ReceiveTransitionsIntentService

private PendingIntent createRequestPendingIntent() {        // If the PendingIntent already exists        if (null != mGeofencePendingIntent) {            // Return the existing intent            return mGeofencePendingIntent;        // If no PendingIntent exists        } else {            // Create an Intent pointing to the IntentService            Intent intent = new Intent("com.aol.androID.geofence.ACTION_RECEIVE_GEOFENCE");//            Intent intent = new Intent(context, ReceiveTransitionsIntentService.class);            /*             * Return a PendingIntent to start the IntentService.             * Always create a PendingIntent sent to Location Services             * with FLAG_UPDATE_CURRENT, so that sending the PendingIntent             * again updates the original. Otherwise, Location Services             * can't match the PendingIntent to requests made with it.             */            return PendingIntent.getbroadcast(                    context,                    0,                    intent,                    PendingIntent.FLAG_UPDATE_CURRENT);        }    }

然后我添加了看起来像这样的GeofenceReceiver类:

public class GeofenceReceiver extends broadcastReceiver {    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){        // Get the error code        int errorCode = LocationClIEnt.getErrorCode(intent);        // Get the error message        String errorMessage = LocationServiceErrorMessages.getErrorString(                context, errorCode);        // Log the error        Log.e(GeofenceUtils.APPTAG,                context.getString(R.string.geofence_Transition_error_detail,                        errorMessage));        // Set the action and error message for the broadcast intent        broadcastIntent                .setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)                .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);        // broadcast the error *locally* to other components in this app        LocalbroadcastManager.getInstance(context).sendbroadcast(                broadcastIntent);    }    private voID handleEnterExit(Intent intent) {        // Get the type of Transition (entry or exit)        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 = GeofenceUtils                    .getTransitionString(Transition);            for (int index = 0; index < geofences.size(); index++) {                Geofence geofence = geofences.get(index);                // ...do something with the geofence entry or exit. I'm saving them to a local sqlite db            }            // 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(MyApplication.getContext())                    .sendbroadcast(broadcastIntent);            // Log the Transition type and a message            Log.d(GeofenceUtils.APPTAG, TransitionType + ": " + IDs);            Log.d(GeofenceUtils.APPTAG,                    context.getString(R.string.geofence_Transition_notification_text));            // In deBUG mode, log the result            Log.d(GeofenceUtils.APPTAG, "Transition");            // An invalID Transition was reported        } else {            // Always log as an error            Log.e(GeofenceUtils.APPTAG,                    context.getString(R.string.geofence_Transition_invalID_type,                            Transition));        }    }    /**     * posts a notification in the notification bar when a Transition is     * detected. If the user clicks the notification, control goes to the main     * Activity.     *      * @param TransitionType     *            The type of Transition that occurred.     *      */    private voID sendNotification(String TransitionType, String locationname) {        // Create an explicit content Intent that starts the main Activity        Intent notificationIntent = new Intent(context, MainActivity.class);        // Construct a task stack        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);        // Adds the main Activity to the task stack as the parent        stackBuilder.addParentStack(MainActivity.class);        // Push the content Intent onto the stack        stackBuilder.addNextIntent(notificationIntent);        // Get a PendingIntent containing the entire back stack        PendingIntent notificationPendingIntent = stackBuilder                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);        // Get a notification builder that's compatible with platform versions        // >= 4        NotificationCompat.Builder builder = new NotificationCompat.Builder(                context);        // Set the notification contents        builder.setSmallicon(R.drawable.ic_notification)                .setContentTitle(TransitionType + ": " + locationname)                .setContentText(                        context.getString(R.string.geofence_Transition_notification_text))                .setContentIntent(notificationPendingIntent);        // Get an instance of the Notification manager        notificationmanager mnotificationmanager = (notificationmanager) context                .getSystemService(Context.NOTIFICATION_SERVICE);        // Issue the notification        mnotificationmanager.notify(0, builder.build());    }}

希望这有助于其他人.

总结

以上是内存溢出为你收集整理的Android Geofence禁用/重新启动位置服务时自动删除全部内容,希望文章能够帮你解决Android Geofence禁用/重新启动位置服务时自动删除所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存