Android wifi地理围栏/后台位置查询

时间:2013-05-04 22:12:43

标签: android geofencing

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

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

让我知道,感谢帮助。

3 个答案:

答案 0 :(得分:6)

答案 1 :(得分: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) {

    }
}

答案 2 :(得分:1)

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