2013-08-23 6 views
1

Je veux suivre l'emplacement de l'utilisateur en continu comme un service d'arrière-plan dans Android, je suis en mesure de le faire en utilisant les services d'arrière-plan et de réception réseau et fournisseur de GPS en fonction de la disponibilité.Basculer entre le réseau et le fournisseur GPS sur le service pas sur l'activité

backgroundService.java

@Override 

     public int onStartCommand(Intent intent, int flags, int startId) { 
     mylistner = new NewLocationListener(getBaseContext()); 

     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 

     criteria.setCostAllowed(true); 
     LocationManager locManager = (LocationManager) getBaseContext() 
     .getSystemService(Context.LOCATION_SERVICE); 
     String bestProvider = locManager.getBestProvider(criteria, false); 
      Toast.makeText(getBaseContext(), "Best Provider "+bestProvider, Toast.LENGTH_SHORT).show(); 
     locManager.requestLocationUpdates(bestProvider, 1000, 5, mylistner); 
     Toast.makeText(getBaseContext(), "Successfully Called", Toast.LENGTH_SHORT).show(); 
     return START_STICKY; 
    } 

Dans un premier temps bestProvider auront GPS ou réseau, mais si c'est GPS d'abord, puis si l'utilisateur se le GPS, il devrait me donner de coordonnées utilisant le réseau. Encore une fois, si l'utilisateur bascule sur GPS, le fournisseur de localisation doit provenir du GPS.

sur le service il y a onProviderEnabled et la méthode onProviderDisabled comment les utiliser dans le but

comme indiqué en tant que solution dans beaucoup question similaire dans le débordement de la pile je ne veux pas envoyer le réseau et fournisseur de GPS du début i vouloir passer selon la disponibilité. est-il réalisable en service. Merci à l'avance

Répondre

0

Cela pourrait vous aider à repenser le problème:

public class GPSTracker extends Service implements android.location.LocationListener { 
    /** 
    * ¨PRIVATE ATTRIBUTES 
    */ 
    // log 
    private static final String TAG        = GPSTracker.class.getSimpleName(); 
    // 
    private Context    _context; 
    private static GPSTracker _instance; 
    // The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0;        // meters 
    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES    = 1000 * 30 * 1;     // minute 
    // Declaring a Location Manager 
    protected LocationManager _locationManager; 
    /** 
    * PUBLIC ATTRIBUTES 
    */ 
    boolean      _isGPSEnabled     = false; 
    boolean      _isNetworkEnabled    = false; 
    boolean      _canGetLocation     = false; 
    public Location    _location; 
    double      _latitude; 
    double      _longitude; 

    public GPSTracker() { 

     _context = null; 
    } 

    public static GPSTracker getInstance() { 

     if (_instance == null) { 
      _instance = new GPSTracker(); 
     } 
     return _instance; 
    } 

    public void set_context(Context context) { 

     this._context = context; 
    } 

    public Location getLocation(Context context) { 

     _context = context; 
     try { 
      _locationManager = (LocationManager) _context.getSystemService(LOCATION_SERVICE); 
      _isGPSEnabled = _locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
      _isNetworkEnabled = _locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
      if (!_isGPSEnabled && !_isNetworkEnabled) { 
       // no network provider is enabled 
      } 
      else { 
       this._canGetLocation = true; 
       if (_isNetworkEnabled) { 
        _locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (_locationManager != null) { 
         _location = _locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
         if (_location != null) { 
          _latitude = _location.getLatitude(); 
          _longitude = _location.getLongitude(); 
         } 
        } 
       } 
       // if GPS Enabled get lat/long using GPS Services 
       if (_isGPSEnabled) { 
        if (_location == null) { 
         _locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
         Log.d("GPS Enabled", "GPS Enabled"); 
         if (_locationManager != null) { 
          _location = _locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
          if (_location != null) { 
           _latitude = _location.getLatitude(); 
           _longitude = _location.getLongitude(); 
          } 
         } 
        } 
       } 
      } 
     } 
     catch (Exception e) { 
      // TODO: ERROR getLocation 
     } 
     return _location; 
    } 

    public void stopUsingGPS() { 

     if (_locationManager != null) { 
      _locationManager.removeUpdates(GPSTracker.this); 
     } 
    } 

    public double getLatitude() { 

     if (_location != null) { 
      _latitude = _location.getLatitude(); 
     } 
     return _latitude; 
    } 

    public double getLongitude() { 

     if (_location != null) { 
      _longitude = _location.getLongitude(); 
     } 
     return _longitude; 
    } 

    /** 
    * Function to check GPS/wifi enabled 
    * 
    * @return boolean 
    */ 
    public boolean canGetLocation() { 

     return this._canGetLocation; 
    } 

    /** 
    * Function to show settings alert dialog On pressing Settings button will lauch Settings Options 
    */ 
    public void showSettingsAlert(String title, String msg) { 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(_context); 
     alertDialog.setTitle(title); 
     alertDialog.setMessage(msg); 
     // Settings button 
     alertDialog.setPositiveButton("Paramètres", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

       Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
       _context.startActivity(intent); 
      } 
     }); 
     // cancel button 
     alertDialog.setNegativeButton("Annuler", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 

       dialog.cancel(); 
      } 
     }); 
     // show 
     alertDialog.show(); 
    } 

    @Override 
    public void onLocationChanged(Location location) { 

     Log.i(TAG, "onLocationChanged--TRACKER--" + location.getLatitude() + "/" + location.getLongitude()); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 

    } 

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

    } 

    @Override 
    public IBinder onBind(Intent arg0) { 

     return null; 
    } 
} 
+1

Merci pour votre temps, mais veulent savoir ici que vous utilisez également réseau et GPS offrent en même temps ne peux pas qu'il soit fait un à la fois . –

+0

Quand j'appelle gtLocation() quel que soit le réseau disponible je l'utilise sinon j'utilise le 3G/... vous pouvez le changer par exemple avec un paramètre lors de l'appel de la fonction (boolean useNetwork) par exemple alors faites un if (useNetwork)/autre ? –

Questions connexes