2017-08-04 110 views
0

在我的项目,我需要:如何创建一个后台服务与间隔时间

  1. 获取用户位置(纬度和长)。
  2. 发送到我的API。
  3. 接收回应。

这一切都在15秒之间。我已经知道如何做这些事情,但它必须在后台。我使用的是JobSchedule,它是完美的,它可以处理一切,连接,网络状态和时间,但问题是,它与旧版本不兼容> 21! 在这些条件下(背景请求和位置),最好的方法是GCMNetworkManager?谢谢!

+0

检查:https://stackoverflow.com/questions/10420358/android-periodic-background-service-advice,看到最后的答案就是你想要的更多 – Vikrant

+0

官方样本为此[链接](https://github.com/googlesamples/android-play-location) – santalu

回答

1

这是你如何做到这一点:

<service 
     android:name=".GeoLocationService" 
     android:exported="false" /> 

服务:

import android.Manifest; 
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.Build; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.support.v4.content.ContextCompat; 
import android.support.v4.content.LocalBroadcastManager; 

public class GeoLocationService extends Service { 

public static final String LOCATION_UPDATE = 
GeoLocationService.class.getSimpleName(); 
public static final String LOCATION_DATA = "location_data"; 

private static final String TAG = GeoLocationService.class.getSimpleName(); 
private LocationManager mLocationManager = null; 
private static final int LOCATION_INTERVAL = 10000; // 30 Seconds 
private static final float LOCATION_DISTANCE = 1f; // meters = 100 m 

    public static void start(Context context) { 
     context.startService(new Intent(context, GeoLocationService.class)); 
    } 

    public static void stop(Context context) { 
     context.stopService(new Intent(context, GeoLocationService.class)); 
    } 

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

     public LocationListener(String provider) { 
      mLastLocation = new Location(provider); 
     } 

     @Override 
     public void onLocationChanged(Location location) { 
      mLastLocation.set(location); 

      Log.i(TAG, "Location Changed! " + location.getLatitude() + " " + location.getLongitude()); 
      Intent intent = new Intent(LOCATION_UPDATE); 
      intent.putExtra(LOCATION_DATA, location); 
      LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
     } 
    } 

    LocationListener[] mLocationListeners = new LocationListener[]{ 
      new LocationListener(LocationManager.GPS_PROVIDER), 
      new LocationListener(LocationManager.NETWORK_PROVIDER) 
    }; 

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

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     super.onStartCommand(intent, flags, startId); 
     return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 
     initializeLocationManager(); 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
        mLocationListeners[1]); 
     } catch (SecurityException ex) { 

     } catch (IllegalArgumentException ex) { 

     } 
     try { 
      mLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE, 
        mLocationListeners[0]); 
     } catch (SecurityException ex) { 

     } catch (IllegalArgumentException ex) { 

     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (mLocationManager != null) { 
      for (int i = 0; i < mLocationListeners.length; i++) { 
       try { 
        if (checkLocationPermission()) { 
         mLocationManager.removeUpdates(mLocationListeners[i]); 
        } 
       } catch (Exception ex) { 

       } 
      } 
     } 
    } 

    private void initializeLocationManager() { 
     if (mLocationManager == null) { 
      mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 
     } 
    } 

    private boolean checkLocationPermission() { 
     if (Build.VERSION.SDK_INT < 23) 
      return true; 

     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_COARSE_LOCATION) 
       != PackageManager.PERMISSION_GRANTED) { 
      return false; 
     } 

     return ContextCompat.checkSelfPermission(this, 
       Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED; 
    } 
} 

,并在您的活动:

private BroadcastReceiver mLocationUpdateMessageReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Location location = intent.getParcelableExtra(GeoLocationService.LOCATION_DATA); 
     //SEND LOCATION TO YOUR API HERE 
    } 
}; 

@Override 
protected void onResume() { 
    super.onResume(); 
    GeoLocationService.start(this); 
    LocalBroadcastManager.getInstance(context).registerReceiver(mLocationUpdateMessageReceiver, 
      new IntentFilter(GeoLocationService.LOCATION_UPDATE)); 
} 


@Override 
protected void onPause() { 
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mLocationUpdateMessageReceiver); 
    GeoLocationService.stop(this); 
    super.onPause(); 
} 
+0

我要尝试适应我的逻辑,cu'z我',使用FusedLocation而不是LocationManager! –

+0

我改变了和工作,唯一的问题是当我打电话'无效停止'它不会停止服务! –

相关问题