Error creating service in the background android

0

How about! I commented: Create a service on Android, but when you start the application it closes and removing the service removes that problem.

Here I start my service:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this, sservicio.class);
    startService(intent);

Here is the sservice class:

public class sservicio extends Service{

    public sservicio() {
    }

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


    @Override
    public void onCreate() {
        Log.d(TAG, "Servicio creado...");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
         new CargarDatos().execute("http://www.cybertodo.mx/WebService.php?nombre=" + etNombres.getText().toString() + "&" + "longitud=" + longitud + "&" + "latitud=" + latitud + "&" + "direccion=" + direccion + ". Ciudad: " + cityName + "&" + "equipo=" + datos);


        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "Servicio destruido...");
    }


}

This class is in the MainActivity.java. I tried only to send a log with the legend that the service was executed but still closes the app. So I put the service in the AndroidManifest.xml:

<service
        android:name=".MainActivity$sservicio"
        android:enabled="true"
        android:exported="true"></service>
    
asked by Juan Trinidad Mayo 29.09.2017 в 02:04
source

1 answer

0

The problem is that sservice is an internal class within MainActivity and therefore Android can not build an instance.

To solve this, you can take the class sservice to a new file or if you prefer to define it within MainActivity , do it as a static class:

public static class sservice extends Service {
    ....
}
    
answered by 29.09.2017 / 02:49
source