mas*_*enk 1 android geofencing
我正在编写一个Android应用程序(不是一个新颖的应用程序),它将根据用户的位置关闭/打开用户的wifi.它将使用网络的位置获取大致位置.我基本上使用Google地图将他们创建的地理围栏保存到我的数据库中.我很好奇什么是最好的方法,确保应用程序不必在前台才能正常工作.
我应该使用addProximityAlert或不同的东西?考虑到我正在创建多个地理围栏并且用户可能有10个设置,我担心为每个地理围栏使用不同的邻近侦听器.相反,每15分钟左右查询位置似乎更节能,然后让应用程序决定它是否在任何这些地理围栏的合理范围内.
让我知道,感谢帮助.
您可以使用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限制:
这是我写的示例代码,它对我来说很好
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 {
}
}
Run Code Online (Sandbox Code Playgroud)
}
然后编写一个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) {
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5946 次 |
| 最近记录: |