首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中从Geofence调用活动

在android中从Geofence调用活动
EN

Stack Overflow用户
提问于 2013-08-13 00:12:31
回答 1查看 391关注 0票数 2

当进入/退出地理围栏区域时,我希望将活动直接带到前台。

我有5个地理围栏区域,并希望当我进入地理围栏区域A(在经纬度中指定)时将ActivityA带到前台,当我在地理围栏时活动B是B,以此类推。(这是一个非消费者应用程序,所以我不介意直接从服务调用活动到前台。)

我在Android的地理围栏上读过this documentation。当不同的活动接收到不同地理围栏区域的ID时,我该如何调用它们?

待定意图:

代码语言:javascript
复制
public class MainActivity extends FragmentActivity {
...

    private PendingIntent getTransitionPendingIntent() {

        Intent intent = new Intent(this,
                ReceiveTransitionsIntentService.class);

        return PendingIntent.getService(
                this,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
    }

...
}

和处理程序的意图:

代码语言:javascript
复制
protected void onHandleIntent(Intent intent) {

    if (LocationClient.hasError(intent)) {

        int errorCode = LocationClient.getErrorCode(intent);

        Log.e("ReceiveTransitionsIntentService",
                "Location Services error: " +
                Integer.toString(errorCode));

    } else {
        // Get the type of transition (entry or exit)
        int transitionType =
                LocationClient.getGeofenceTransition(intent);
        // Test that a valid transition was reported
        if (
            (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER)
             ||
            (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT)
           ) {
            List <Geofence> triggerList =
                    getTriggeringGeofences(intent);

            String[] triggerIds = new String[geofenceList.size()];

            for (int i = 0; i < triggerIds.length; i++) {
                // Store the Id of each geofence
                triggerIds[i] = triggerList.get(i).getRequestId();
            }

        }

    } else {
        Log.e("ReceiveTransitionsIntentService",
                "Geofence transition error: " +
                Integer.toString()transitionType));
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-02-28 23:28:30

documentation声明你不应该这样做:

从位置服务发送的意图可以触发应用程序中的各种操作,但您不应该让它启动活动或片段,因为组件应该仅在响应用户操作时才可见。在许多情况下,IntentService是处理意图的好方法。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18191786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档