在特定位置的范围内时自动打开我的应用程序



我正在使用intent切换到Google Maps,以获取到在我的应用程序中实现的地图上显示的标记的路线。我的问题是,是否有任何方法可以让我的应用程序在后台以特定的时间间隔发送位置,这样当用户靠近该位置时,当应用程序重新打开以直接更新位置时,我就可以实现应用程序的下一步。(应用程序应该在他接近时做点什么(

您可以尝试创建和监控地理围栏,以检测用户是否接近该位置:

public class MainActivity extends AppCompatActivity {
// ...
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (geofencePendingIntent != null) {
return geofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
// calling addGeofences() and removeGeofences().
geofencePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.
FLAG_UPDATE_CURRENT);
return geofencePendingIntent;
}

然后,当用户接近该位置时,直接从您的应用程序开始跟踪它。类似的东西:

public class GeofenceBroadcastReceiver extends BroadcastReceiver {
// ...
protected void onReceive(Context context, Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceStatusCodes.getErrorString(geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
this,
geofenceTransition,
triggeringGeofences
);
// Start tracking user position directly here
...
} else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// Stop tracking user position directly here
...
else {
// Log the error.
Log.e(TAG, getString(R.string.geofence_transition_invalid_type,
geofenceTransition));
}
}
}

我不确定它是否有效,但你可以试试这个。我的猜测应该有效。

ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.moveTaskToFront(getTaskId(), 0);

请注意,当系统大于或等于android O 时,您无法在后台保持获取用户位置

后台执行限制

解决方案:您可以在前台注册服务
步骤1:创建您自己的服务
步骤2:向Androidmanifest注册
第3步:创建BroadcastReceiver接收器数据

来自谷歌的前景示例

相关内容

  • 没有找到相关文章

最新更新