Android - 在特定时间间隔内获取用户当前位置

时间:2017-09-06 05:45:09

标签: java android

即使关闭应用程序,我也需要在特定时间间隔内使用gps获取用户当前位置。我已经完成了使用服务 这是我的服务类。

public class MyService extends Service {

    private static final String TAG = "BOOMBOOMTESTGPS";
    private LocationManager mLocationManager = null;
    private static final int LOCATION_INTERVAL = 1000;
    private static final float LOCATION_DISTANCE = 0f;

    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, "LocationListener " + location);
        }

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

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

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

    android.location.LocationListener[] mLocationListener = new android.location.LocationListener[]{
            new LocationListener(LocationManager.GPS_PROVIDER), new LocationListener(LocationManager.NETWORK_PROVIDER)
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        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");
        initializeLocationManagrr();
        try {

            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, mLocationListener[0]);

        }catch(java.lang.SecurityException ex){
            Log.i(TAG, "fail to request location update, ignore", ex);
        } catch (IllegalArgumentException ex1){
            Log.d(TAG, "gps provider does not exist " + ex1.getMessage());
        }
    }

    @Override
    public void onDestroy() {
        Log.e(TAG,"onDestroy");
        super.onDestroy();
        if(mLocationManager!=null){
            for(int i=0;i<mLocationListener.length;i++){
                try{
                    mLocationManager.removeUpdates(mLocationListener[i]);
                }catch ( Exception e2){
                    Log.i(TAG, "fail to remove location listners, ignore", e2);
                }
            }
        }
    }
    private void initializeLocationManagrr(){
        Log.e(TAG,"initializeLocationManagaer");
        if(mLocationManager==null){
            mLocationManager=(LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        }
    }
 }

我正在从MainActivity.java这样调用这个类

startService(new Intent(MainActivity.this,MyService.class));

并且在我的日志中没有显示有关该位置的信息。

此外,我还在我的应用中添加了google服务配置文件 我的主要问题是我的Log cat中没有任何用户位置,但应用程序运行正常。

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

您可以使用LocationListener,当您的位置发生变化时会调用它,此外您也可以提供时间间隔

LocationManager mlocManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener(context);

mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1,1,mlocL istener);

// 语法:mlocManager.requestLocationUpdates(provider,minTime,minDistance,listener)

您可以在此方法中设置时间和距离,请参阅语法