具有模拟位置提供程序的Android geofence
我已经开始在Android上开发最后一个位置服务功能:Geofenses!!模拟位置提供程序是否存在任何已知问题?下面的示例()即使当前位置在geofence内,my intent服务也从未启动。我正在使用FakeGPS android应用程序作为模拟位置提供商,如果我模拟一条路线,我会在谷歌地图应用程序上看到位置的变化,因此模拟位置提供商运行良好。有什么想法吗 谢谢。具有模拟位置提供程序的Android geofence,android,android-location,android-geofence,Android,Android Location,Android Geofence,我已经开始在Android上开发最后一个位置服务功能:Geofenses!!模拟位置提供程序是否存在任何已知问题?下面的示例()即使当前位置在geofence内,my intent服务也从未启动。我正在使用FakeGPS android应用程序作为模拟位置提供商,如果我模拟一条路线,我会在谷歌地图应用程序上看到位置的变化,因此模拟位置提供商运行良好。有什么想法吗 谢谢。 保罗。我一直在努力让它发挥作用。多痛苦的谷歌!因为它说GeoFence可以很容易地使用模拟进行测试 魔术是在传递给setMoc
保罗。我一直在努力让它发挥作用。多痛苦的谷歌!因为它说GeoFence可以很容易地使用模拟进行测试 魔术是在传递给setMockLocation的位置中使用提供者名称“network”
Location location = new Location("network");
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setTime(new Date().getTime());
location.setAccuracy(3.0f);
location.setElapsedRealtimeNanos(System.nanoTime());
LocationServices.FusedLocationApi.setMockLocation(_googleApiClient, location);
确保在手机上启用模拟位置。选择设置->开发人员选项->允许模拟位置 地理围栏使用FusedLocationProviderApi,因此要模拟它们,必须使用
您可以使用广播接收器而不是像这样的活动
public class GeofenceReceiver extends BroadcastReceiver
implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
ResultCallback<Status>{
GoogleApiClient mGoogleApiClient;
PendingIntent mGeofencePendingIntent ;
Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
mGoogleApiClient = new GoogleApiClient.Builder(mContext)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
try {
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
// The GeofenceRequest object.
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().
} catch (SecurityException securityException) {
Log.i(getClass().getSimpleName(),securityException.getMessage());
}
}
// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
/**
* Runs when the result of calling addGeofences() and removeGeofences() becomes available.
* Either method can complete successfully or with an error.
*
* Since this activity implements the {@link ResultCallback} interface, we are required to
* define this method.
*
* @param status The Status returned through a PendingIntent when addGeofences() or
* removeGeofences() get called.
*/
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess()) {
Log.i(getClass().getSimpleName(),"Success");
} else {
// Get the status code for the error and log it using a user-friendly message.
Log.i(getClass().getSimpleName(),getErrorString(status.getStatusCode()));
}
}
private GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL);
builder.addGeofences(getGeofecne());
return builder.build();
}
private List<Geofence> getGeofecne(){
List<Geofence> mGeofenceList = new ArrayList<>();
//add one object
mGeofenceList.add(new Geofence.Builder()
// Set the request ID of the geofence. This is a string to identify this
// geofence.
.setRequestId("key")
// Set the circular region of this geofence.
.setCircularRegion(
25.768466, //lat
47.567625, //long
50) // radios
// Set the expiration duration of the geofence. This geofence gets automatically
// removed after this period of time.
//1000 millis * 60 sec * 5 min
.setExpirationDuration(1000 * 60 * 5)
// Set the transition types of interest. Alerts are only generated for these
// transition. We track entry and exit transitions in this sample.
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_DWELL)
//it's must to set time in millis with dwell transition
.setLoiteringDelay(3000)
// Create the geofence.
.build());
return mGeofenceList;
}
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(mContext, GeofenceTransitionsIntentService.class);
return PendingIntent.getService(mContext, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
}
公共类GeofenceReceiver扩展了BroadcastReceiver
工具
GoogleAppClient.ConnectionCallbacks,
GoogleAppClient.OnConnectionFailedListener,
结果回拨{
GoogleapClient MGoogleapClient;
悬挂式帐篷管理围栏悬挂式帐篷;
语境;
@凌驾
公共void onReceive(上下文、意图){
mContext=上下文;
mgoogleapclient=新的Googleapclient.Builder(mContext)
.addOnConnectionFailedListener(此)
.addConnectionCallbacks(此)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@凌驾
未连接的公共无效(@Nullable Bundle){
试一试{
LocationServices.GeofencingApi.AddGeofines(
MGoogleapClient,
//GeofenceRequest对象。
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(this);//在onResult()中处理的结果。
}捕获(SecurityException SecurityException){
Log.i(getClass().getSimpleName(),securityException.getMessage());
}
}
//如果应用程序未使用“访问\罚款\位置”权限,则生成捕获异常。
@凌驾
公共空间连接暂停(int i){
}
@凌驾
public void onconnection失败(@NonNull ConnectionResult ConnectionResult){
}
/**
*在调用addGeofences()和removeGeofences()的结果可用时运行。
*这两种方法都可以成功完成,也可以出现错误。
*
*由于此活动实现了{@link ResultCallback}接口,我们需要
*定义此方法。
*
*@param status addgeofines()或
*调用removeGeofences()。
*/
@凌驾
public void onResult(@NonNull状态){
if(status.issucess()){
Log.i(getClass().getSimpleName(),“Success”);
}否则{
//获取错误的状态代码,并使用用户友好的消息进行记录。
Log.i(getClass().getSimpleName(),getErrorString(status.getStatusCode());
}
}
私人GeofencingRequest getGeofencingRequest(){
GeofencingRequest.Builder=新的GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_驻留);
addgeofenses(getGeofecne());
返回builder.build();
}
私有列表getGeofecne(){
List mGeofenceList=newarraylist();
//添加一个对象
mGeofenceList.add(new geofinence.Builder()
//设置地理围栏的请求ID。这是一个用于标识此项的字符串
//地球围栏。
.setRequestId(“密钥”)
//设置此地理围栏的圆形区域。
.setCircularRegion(
25.768466,//纬度
47.567625,//长
50)//收音机
//设置地理围栏的过期时间。此地理围栏将自动
//在这段时间后删除。
//1000毫秒*60秒*5分钟
.setExpirationDuration(1000*60*5)
//设置感兴趣的转换类型。仅为这些类型生成警报
//转换。我们在这个示例中跟踪进入和退出转换。
.setTransitionTypes(
土工围栏。土工围栏(过渡区)
//必须以毫秒为单位设置停留时间
.setLoiteringDelay(3000)
//创建地理围栏。
.build());
返回mGeofenceList;
}
私有PendingEvent GetGeoFencePendingEvent(){
//如果我们已经有了悬挂式帐篷,就重新使用它。
if(mgeofencependingent!=null){
返回mgeofencependingent;
}
意向意向=新意向(mContext、GeofenceTransitionsIntentService.class);
返回pendingent.getService(mContext,0,intent,pendingent)。
标志(更新)(当前);;
}
}
看看我的回购协议,这里有一个使用geofence的完整示例
您不需要先调用LocationServices.FusedLocationApi.setMockMode(\u GoogleapClient,true)吗?您是否有一个链接显示LocationServices.GeofencingApi实际使用LocationServices.FusedLocationApi进行模拟?我没有看到任何证据表明这是真的。欢迎链接到解决方案,但请确保没有它,您的答案是有用的:这样您的其他用户就会知道它是什么以及为什么存在,然后引用您链接到的页面的最相关部分,以防目标页面不可用。而这个链接可能是