【问题标题】:calling Activities from Geofence in android在android中从地理围栏调用活动
【发布时间】:2013-08-12 16:12:31
【问题描述】:

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

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

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

待定意图:

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);
    }

...
}

以及处理程序意图:

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));
    }
}

【问题讨论】:

    标签: android android-geofence


    【解决方案1】:

    documentation 声明您不应该这样做:

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

    【讨论】:

    • 他正在使用意图服务
    猜你喜欢
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多