2017-02-01 3 views
0

Der Dienst ist auch nach dem Schließen der Anwendung noch aktiv.Ein Intervalldienst kann nicht geschlossen werden

Dienst starten:

@Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_promotion); 
 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     setSupportActionBar(toolbar); 
 

 
     //Service 
 
     Calendar cal = Calendar.getInstance(); 
 
     cal.add(Calendar.SECOND, 10); 
 
     Intent intent1 = new Intent(this, PositionService.class); 
 
     PendingIntent pintent = PendingIntent.getService(this, 0, intent1, 0); 
 
     AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
 
     alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 15 * 1000, pintent); 
 
     startService(new Intent(getBaseContext(), PositionService.class)); 
 
    }

Die Klasse Service

public class PositionService extends Service { 
 
    String id_user; 
 
    private String JSON_STRING; 
 
    double lat; 
 
    double lon; 
 
    int id_promotions; 
 
    String description_1; 
 
    String promotionDetail; 
 
    String address; 
 
    double latPromotion; 
 
    double lonPromotion; 
 

 
    //notification 
 
    // private static final String TAG = "MyService"; 
 

 

 

 
    @Override 
 
    public IBinder onBind(Intent intent) { 
 
     // TODO: Return the communication channel to the service. 
 
     throw new UnsupportedOperationException("Not yet implemented"); 
 
    } 
 

 
    @Override 
 
    public void onCreate() { 
 
     // TODO Auto-generated method stub 
 
     SharedPreferences sharedPref= getSharedPreferences("mypref", 0); 
 
     id_user = sharedPref.getString("id_user", ""); 
 
     super.onCreate(); 
 
    } 
 

 
    @Override 
 
    public void onDestroy() { 
 
     // TODO Auto-generated method stub 
 
     super.onDestroy(); 
 
     //stopService(new Intent(this, PositionService.class)); 
 
    } 
 

 
    @Override 
 
    public void onTaskRemoved(Intent rootIntent) { 
 
     super.onTaskRemoved(rootIntent); 
 
     //stopService(new Intent(this, PositionService.class)); 
 
    } 
 

 

 
    @Override 
 
    public int onStartCommand(Intent intent, int flags, int startId) { 
 
     // TODO Auto-generated method stub 
 
     Toast.makeText(getApplicationContext(), "Service Running", Toast.LENGTH_SHORT).show(); 
 

 
     /* Uso de la clase LocationManager para obtener la localizacion del GPS */ 
 
     LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
 
     Localizacion Local = new Localizacion(); 
 
     Local.setMainActivity(this); 
 
     mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local); 
 

 

 
     return super.onStartCommand(intent, flags, startId); 
 
    } 
 

 
    public void setLocation(Location loc) { 
 
     //Obtener la direccion de la calle a partir de la latitud y la longitud 
 
     if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) { 
 
      try { 
 
       Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
 
       List<Address> list = geocoder.getFromLocation(
 
         loc.getLatitude(), loc.getLongitude(), 1); 
 
       if (!list.isEmpty()) { 
 
        Address DirCalle = list.get(0); 
 
        //textViewAddress.setText(DirCalle.getAddressLine(0)); 
 
       } 
 

 
      } catch (IOException e) { 
 
       e.printStackTrace(); 
 
      } 
 
     } 
 
    } 
 

 
    
 
    public class Localizacion implements LocationListener { 
 
     PositionService positionService; 
 

 
     public PositionService getPositionService() { 
 
      return positionService; 
 
     } 
 

 
     public void setMainActivity(PositionService positionService) { 
 
      this.positionService = positionService; 
 
     } 
 

 
     @Override 
 
     public void onLocationChanged(Location loc) { 
 
      // Este metodo se ejecuta cada vez que el GPS recibe nuevas coordenadas 
 
      // debido a la deteccion de un cambio de ubicacion 
 

 
      loc.getLatitude(); 
 
      loc.getLongitude(); 
 
      lat = loc.getLatitude(); 
 
      lon = loc.getLongitude(); 
 
      //Toast.makeText(getApplicationContext(), "latitud: " + lat + ", longitud: " + lon, Toast.LENGTH_SHORT).show(); 
 
      //textViewLatitud.setText(String.valueOf(lat)); 
 
      //textViewLongitud.setText(String.valueOf(lon)); 
 

 
      this.positionService.setLocation(loc); 
 
     } 
 

 
     @Override 
 
     public void onProviderDisabled(String provider) { 
 
      // Este metodo se ejecuta cuando el GPS es desactivado 
 
      //textViewLatitud.setText("GPS Desactivado"); 
 
     } 
 

 
     @Override 
 
     public void onProviderEnabled(String provider) { 
 
      // Este metodo se ejecuta cuando el GPS es activado 
 
      //textViewLatitud.setText("GPS Activado"); 
 
     } 
 

 
     @Override 
 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
 
      // Este metodo se ejecuta cada vez que se detecta un cambio en el 
 
      // status del proveedor de localizacion (GPS) 
 
      // Los diferentes Status son: 
 
      // OUT_OF_SERVICE -> Si el proveedor esta fuera de servicio 
 
      // TEMPORARILY_UNAVAILABLE -> Temporalmente no disponible pero se 
 
      // espera que este disponible en breve 
 
      // AVAILABLE -> Disponible 
 
     } 
 

 
    }/* Fin de la clase localizacion */ 
 

 
    
 

 
}

Ich versuche Service mit diesem zu beenden, aber funktioniert nicht

else if (id == R.id.nav_send) { 
 
      stopService(new Intent(this, PositionService.class)); 
 
      finish(); 
 

 
     }

Das Manifest:

 <service 
 
      android:name=".service.PositionService" 
 
      android:enabled="true" 
 
      android:exported="true"> 
 
     </service>
ich mit Toast de service Lauf kontrollieren, aber strop nie

+1

wo ist Ihr Service-Klasse-Code? – W4R10CK

+0

Aktualisieren Sie die Info –

+0

auch nach dem Stoppen des Dienstes oder nach dem Beenden der App? – W4R10CK

Antwort

0

Nun, das Problem der Alarm, dass der Dienst gestartet wird.

Intent intent = new Intent(PromotionActivity.this, PositionService.class); 
 
      PendingIntent pintent = PendingIntent.getService(PromotionActivity.this, 0, intent, 0); 
 
      AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
 
      alarm.cancel(pintent);

1

hinzufügen START_NOT_STICKY

Danke für die Hilfe läuft , welches wird verhindern, dass die Service wieder zu starten:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(), "Service Running", Toast.LENGTH_SHORT).show(); 

    /* Uso de la clase LocationManager para obtener la localizacion del GPS */ 
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Localizacion Local = new Localizacion(); 
    Local.setMainActivity(this); 
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local); 


    return START_NOT_STICKY; //here 
} 
+0

Danke für die Antwort, der Dienst scheint sehr aggressiv, läuft weiter mit der vorgeschlagenen Zeilencode, gibt es eine andere Möglichkeit? –

+0

Läuft es immer noch mit Toast-Nachricht, auch nach dem Beenden der App? – W4R10CK

+0

Ja mein Freund, der Service läuft weiter –

Verwandte Themen