2017-08-04 66 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

我改變了和工作,唯一的問題是當我打電話'無效停止'它不會停止服務! –

相關問題