在Android中调用Geofence的活动

时间:2013-08-12 16:12:31

标签: android android-geofence

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

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

我在Android上阅读了关于Geofencing的this documentation。当他们收到地理围栏区域的不同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);
    }

...
}

Handler Intent:

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

1 个答案:

答案 0 :(得分:-1)

documentation表示你不应该这样做:

  

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