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

Android 在特定位置范围内自动打开我的应用程序,android,google-maps,Android,Google Maps,我正在使用意图切换到谷歌地图,以获取到我的应用程序中实现的地图上显示的标记的路线。我的问题是,是否有任何方法可以让我的应用程序在后台以一定的时间间隔发送位置,这样当用户靠近位置时,当应用程序重新打开以直接更新位置时,我就能够执行应用程序的下一步。(当他靠近时,应用程序应该做些事情)我不确定它是否有效,但你可以试试这个。我想应该行得通 ActivityManager activityManager = (ActivityManager) getApplicationContext()

我正在使用
意图
切换到
谷歌地图
,以获取到我的应用程序中实现的地图上显示的标记的路线。我的问题是,是否有任何方法可以让我的应用程序在后台以一定的时间间隔发送位置,这样当用户靠近位置时,当应用程序重新打开以直接更新位置时,我就能够执行应用程序的下一步。(当他靠近时,应用程序应该做些事情)

我不确定它是否有效,但你可以试试这个。我想应该行得通

ActivityManager activityManager = (ActivityManager) getApplicationContext()
        .getSystemService(Context.ACTIVITY_SERVICE);

activityManager.moveTaskToFront(getTaskId(), 0);

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

解决方案:您可以在前台注册服务。
步骤1:创建您自己的服务
步骤2:向Androidmanifest注册
步骤3:创建广播接收器数据
步骤4:在onResume绑定和onstop解除绑定服务中

您可以尝试检测用户是否靠近该位置:

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));
        }
    }
}
公共类GeofenceBroadcastReceiver扩展了BroadcastReceiver{
// ...
受保护的void onReceive(上下文、意图){
GeofencingEvent GeofencingEvent=GeofencingEvent.fromIntent(intent);
if(geofencingEvent.hasError()){
String errorMessage=GeofenceStatusCodes.getErrorString(geofencingEvent.getErrorCode());
Log.e(标签、错误消息);
返回;
}
//获取转换类型。
int geofenceTransition=geofencingEvent.getGeofenceTransition();
//测试报告的转换是否有意义。
如果(geofenceTransition==Geofence.Geofence\u TRANSITION\u ENTER){
//获取触发的geofences。单个事件可以触发
//多重地理围栏。
List triggeringGeofences=geofencingEvent.getTriggeringGeofences();
//以字符串形式获取转换详细信息。
字符串geofenceTransitionDetails=getGeofenceTransitionDetails(
这
地球围栏过渡,
触发地理围栏
);
//直接在这里开始跟踪用户位置
...
}else if(geofenceTransition==Geofence.Geofence\u TRANSITION\u EXIT){
//停止在此处直接跟踪用户位置
...
否则{
//记录错误。
Log.e(标记,getString(R.string.geofence)\u转换\u无效\u类型,
地球围栏(过渡);
}
}
}

你能给我一点关于实现这一点的更多信息吗?我实际上解决了距离问题。我在
onResume
中使用了
回调
,并在服务器端进行了计算。现在我想知道当
谷歌地图
导航打开时,如何从后台更新位置