1

我正在编写一个 Android 应用程序(不是一个新颖的应用程序),它将根据用户的位置关闭/打开用户的 wifi。它将使用网络的位置获取大致位置。我基本上使用谷歌地图将他们创建的地理围栏保存到我的数据库中。我很好奇在确保应用程序不必处于前台以使其正常工作方面的最佳方法是什么。

我应该使用addProximityAlert还是不同的东西?考虑到我正在创建多个地理围栏并且用户可能设置了 10 个,我担心为每个地理围栏使用不同的邻近侦听器。相反,每 15 分钟左右查询一次位置,然后让应用程序决定它是否在任何这些地理围栏的合理范围内似乎更省电。

让我知道,感谢您的帮助。

4

3 回答 3

6

这是我编写的示例代码,对我来说效果很好

public class LocationClientService extends Service implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener,
        LocationClient.OnAddGeofencesResultListener {

private LocationClient mLocationClient;
private List<Geofence> mGeofenceLists = new ArrayList<Geofence>();

@Override
public void onCreate() {
    super.onCreate();

    Geofence geofence1 = new Geofence.Builder()
            .setRequestId("your target place")
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
            .setCircularRegion(0.0, 0.0, 2000.0f)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .build();

    mGeofenceLists.add(geofence1);

    mLocationClient = new LocationClient(this, this, this);
    mLocationClient.connect();
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private PendingIntent getPendingIntent() {
    Intent intent = new Intent(this, TransitionsIntentService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void onConnected(Bundle bundle) {
    mLocationClient.addGeofences(mGeofenceLists, getPendingIntent(), this);
}

@Override
public void onDisconnected() {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}

@Override
public void onDestroy() {
    mLocationClient.disconnect();
    super.onDestroy();
}

@Override
public void onAddGeofencesResult(int i, String[] strings) {
    if (LocationStatusCodes.SUCCESS == i) {
        //todo check geofence status
    } else {
    }
}

}

然后编写一个 IntentService 来接收地理围栏进入或退出:

public class TransitionsIntentService extends IntentService {
    public static final String TRANSITION_INTENT_SERVICE = "ReceiveTransitionsIntentService";

    public TransitionsIntentService() {
        super(TRANSITION_INTENT_SERVICE);
    }

@Override
protected void onHandleIntent(Intent intent) {
    if (LocationClient.hasError(intent)) {
        //todo error process
    } else {
        int transitionType = LocationClient.getGeofenceTransition(intent);
        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ||
                transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) {
            List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
            for (Geofence geofence : triggerList) {
                Log.i("test", "triggered Id " + geofence.getRequestId());
            }
        }
        generateNotification(transitionType);
    }
}

    private void generateNotification(int type) {

    }
}
于 2013-06-12T22:27:58.853 回答
6

您可以使用 Google 刚刚发布的全新 GeoFence 功能。它支持 API 8+。类参考:

http://developer.android.com/reference/com/google/android/gms/location/Geofence.html

指导:

http://developer.android.com/training/location/geofencing.html

编辑:在 LocationStatusCodes 中提到了 GeoFences 限制:

http://developer.android.com/reference/com/google/android/gms/location/LocationStatusCodes.html#GEOFENCE_TOO_MANY_GEOFENCES

于 2013-05-16T20:14:35.140 回答
1

如果您有兴趣,可以查看此 IO 会话以查看功能和限制的概述: https ://www.youtube.com/watch?v= Bte_GHuxUGc 地理围栏大约是 23:15

于 2013-08-30T14:48:19.423 回答