How to leave a service running in the background on Android by closing the application that starts it

3

From an Android application I create and execute a service that runs on a thread and runs in the background, but when the main application is completely closed, the service also dies.

The thread I think I declare as Daemon but still "kill" the service.

My idea is to leave the service running even if we close the main application, a wasap style that when you close the main program the messages keep coming to you.

Any ideas?

    
asked by Pablo Simon DiEstefano 25.05.2017 в 13:39
source

2 answers

4

If you want to use a service and this continues executing the process even destroying the application , you must define:

  

Service.START_STICKY : Recreate the service if the application is   destroy.

You can see as an example this service that plays an audio file:

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;


/* Add declaration of this service into the AndroidManifest.xml inside application tag*/

public class BackgroundSoundService extends Service {

    private static final String TAG = "BackgroundSoundService";
    MediaPlayer player;

    public IBinder onBind(Intent arg0) {
        Log.i(TAG, "onBind()" );
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.jorgesys_song);
        player.setLooping(true); 
        player.setVolume(100,100);
        Toast.makeText(this, "Service started...", Toast.LENGTH_SHORT).show();
        Log.i(TAG, "onCreate() , service started...");

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return Service.START_STICKY;
    }

    public IBinder onUnBind(Intent arg0) {
        Log.i(TAG, "onUnBind()");
        return null;
    }

    public void onStop() {
        Log.i(TAG, "onStop()");
    }
    public void onPause() {
        Log.i(TAG, "onPause()");
    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
        Toast.makeText(this, "Service stopped...", Toast.LENGTH_SHORT).show();
        Log.i(TAG, "onCreate() , service stopped...");
    }

    @Override
    public void onLowMemory() {
        Log.i(TAG, "onLowMemory()");
    }
}

You can also use a BroadcastReceiver

    
answered by 25.05.2017 / 18:25
source
1

DO NOT use threads

because they die when the app is closed.

I recommend you use an extended class to Service

    public class ClaseParaEjecutarEnSegundoPlano extends Service {


@Override
public void onCreate() {
    super.onCreate();

    Log.i( "msg", "Servicio creado");
}

@Override
public int onStartCommand(Intent intencion, int flags, int idArranque) {
    Log.i( "msg", "Servicio reiniciado");

    //----- Aquí tu codigo--------
    return START_NOT_STICKY;
}

@Override
public void onDestroy() {
    super.onDestroy();
}

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

}

You start the service from the Activity

with:

    startService(new Intent(this, ClaseParaEjecutarEnSegundoPlano.class));

and to stop it:

    stoptService(new Intent(this, ClaseParaEjecutarEnSegundoPlano.class));
    
answered by 25.05.2017 в 17:26