2013-05-04 101 views
1

我正在编写一个Android应用程序(不是小说),它会根据用户的位置关闭用户的无线网络。它将使用网络的位置获得大概的位置。我基本上使用谷歌地图来保存他们创建到我的数据库的地理栅栏。我很好奇什么是确保应用程序不必在前台才能正常工作的最佳方法。Android无线地理栅栏/背景位置查询

我应该使用addProximityAlert还是别的不同?考虑到我正在创建多个地理栅栏,并且用户可能已经设置了10个,我担心为每个地理围栏使用不同的邻近侦听器。相反,每隔15分钟左右查询一次位置信息看起来效率更高,然后让应用程序确定它是否在任何地理围栏的合理范围内。

让我知道,感谢帮助。

回答

6
+0

那么整个最后的项目有很多小时花费在后期工作,它被熟成Android。只是我的运气:)感谢您的建议,但可能会改写它。 – maschwenk 2013-05-16 21:52:13

+0

你有没有参考100 geofences的限制?我无法找到有关此限制的任何信息或线索...... – usb79 2013-06-11 16:13:39

+1

@ usb79虽然开发人员表示会对Geofences的限制做出澄清,但在Google I/O中提到了它。他们可能已经将其删除。我会编辑我的答案。 – Ejmedina 2013-06-11 19:51:25

6

这里是样品c颂我写的,它的工作对我罚款

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) { 

    } 
} 
+0

https://github.com/chenjishi/android_location_demo这是一个演示如何使用android提供的新位置API,包括融合位置,地理范围和活动识别的示例代码。 – 2013-07-23 17:28:40

1

如果你有兴趣,你可以看看这个IO会议以查看功能和限制的概述: https://www.youtube.com/watch?v=Bte_GHuxUGc Geofencing大约23:15

+0

链接已死:( – Raul 2015-09-22 20:48:25

+0

@Raul我在YouTube上找到了视频并更新了链接。 – PSIXO 2015-09-23 09:08:14