我可以通过始终在后台运行的服务跟踪 gps 纬度和经度吗?

Posted

技术标签:

【中文标题】我可以通过始终在后台运行的服务跟踪 gps 纬度和经度吗?【英文标题】:Can I tracking gps latitude and longitude by a service that always run in background? 【发布时间】:2019-02-21 06:56:06 【问题描述】:

我想通过在后台和应用程序未运行时执行的服务获取纬度和经度。 有没有人用一个例子来指导我? 最重要的是,当程序完全关闭时,服务会正常工作并显示经纬度。 我的请求与以下链接非常相​​似。

getting latitude and longitude using gps every 10 minute in background android

我在后台或程序关闭时寻找的所有应用程序均未正确执行。

谢谢

【问题讨论】:

【参考方案1】:

尝试使用此代码在后台服务中定期获取位置更新

更改 LOCATION_INTERVAL &

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

public class MyLocationService extends Service 
    private static final String TAG = "MyLocationService";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 10f;

    private class LocationListener implements android.location.LocationListener 
        Location mLastLocation;

        public LocationListener(String provider) 
            Log.e(TAG, "LocationListener " + provider);
            mLastLocation = new Location(provider);
        

        @Override
        public void onLocationChanged(Location location) 
            Log.e(TAG, "onLocationChanged: " + location);
            mLastLocation.set(location);
        

        @Override
        public void onProviderDisabled(String provider) 
            Log.e(TAG, "onProviderDisabled: " + provider);
        

        @Override
        public void onProviderEnabled(String provider) 
            Log.e(TAG, "onProviderEnabled: " + provider);
        

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) 
            Log.e(TAG, "onStatusChanged: " + provider);
        
    

    /*
    LocationListener[] mLocationListeners = new LocationListener[]
            new LocationListener(LocationManager.GPS_PROVIDER),
            new LocationListener(LocationManager.NETWORK_PROVIDER)
    ;
    */

    LocationListener[] mLocationListeners = new LocationListener[]
            new LocationListener(LocationManager.PASSIVE_PROVIDER)
    ;

    @Override
    public IBinder onBind(Intent arg0) 
        return null;
    

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
        Log.e(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    

    @Override
    public void onCreate() 

        Log.e(TAG, "onCreate");

        initializeLocationManager();

        try 
            mLocationManager.requestLocationUpdates(
                    LocationManager.PASSIVE_PROVIDER,
                    LOCATION_INTERVAL,
                    LOCATION_DISTANCE,
                    mLocationListeners[0]
            );
         catch (java.lang.SecurityException ex) 
            Log.i(TAG, "fail to request location update, ignore", ex);
         catch (IllegalArgumentException ex) 
            Log.d(TAG, "network provider does not exist, " + ex.getMessage());
        

        /*try 
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    LOCATION_INTERVAL,
                    LOCATION_DISTANCE,
                    mLocationListeners[1]
            );
         catch (java.lang.SecurityException ex) 
            Log.i(TAG, "fail to request location update, ignore", ex);
         catch (IllegalArgumentException ex) 
            Log.d(TAG, "gps provider does not exist " + ex.getMessage());
        */
    

    @Override
    public void onDestroy() 
        Log.e(TAG, "onDestroy");
        super.onDestroy();
        if (mLocationManager != null) 
            for (int i = 0; i < mLocationListeners.length; i++) 
                try 
                    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
                        return;
                    
                    mLocationManager.removeUpdates(mLocationListeners[i]);
                 catch (Exception ex) 
                    Log.i(TAG, "fail to remove location listener, ignore", ex);
                
            
        
    

    private void initializeLocationManager() 
        Log.e(TAG, "initializeLocationManager - LOCATION_INTERVAL: "+ LOCATION_INTERVAL + " LOCATION_DISTANCE: " + LOCATION_DISTANCE);
        if (mLocationManager == null) 
            mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        
    

【讨论】:

你可以在正确的地方添加一个 toast 以在应用程序未运行时显示 lat 和 lon 吗?当应用程序运行时一切正常,但是当应用程序关闭时我的服务停止了。 您在哪个 android 版本上运行此代码?

以上是关于我可以通过始终在后台运行的服务跟踪 gps 纬度和经度吗?的主要内容,如果未能解决你的问题,请参考以下文章

如何在应用程序处于后台时保持GPS接收器位置更新(Google Play服务位置API)

安卓修改GPS经纬度

在 PHP 中计算纬度/经度 GPS 点周围的半径

IOS的多方实时GPS跟踪[关闭]

平板电脑处于待机状态时,我的 GPS 应用程序是不是可以继续在后台运行?

在后台运行应用程序,如谷歌纬度