services on android

1

I'm working on an android application that runs a servicio in the background, I've been testing with different services and it happens that all of them run fine while the app runs normally, when I close the application the service will stop. Does anyone know why this happens?

public class serviceMusic extends Service{
    private MediaPlayer med;
    private Timer mTimer = null;

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

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


        this.mTimer = new Timer();

    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        med= MediaPlayer.create(this, R.raw.uci);
        med.start();

        return START_STICKY;
    }

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

logcat that generates when I close the app

  

04-11 08: 56: 54.133 2263-2263 / com.example.root.primera D / MediaPlayer:   msg handleMessage: (4, 0, 0) 04-11 08: 57: 11.352   2263-2263 / com.example.root.primera D / OpenGLRenderer: Flushing caches   (mode 0) 04-11 08: 57: 11.369 2263-2263 / com.example.root.primera   D / ActivityThread: ACT-DESTROY_ACTIVITY handled: 1 /   android.os.BinderProxy@41e082d0 04-11 08: 57: 11.369 2263-2263 /?   D / ActivityThread: SVC-SERVICE_ARGS handled: 0 /   ServiceArgsData{token=android.os.BinderProxy@41e98f08 startId = 2   args = Intent {act = android.intent.action.MAIN   cat = [android.intent.category.LAUNCHER] flg = 0x10000000   cmp = com.example.root.primera / .MainActivity}}

    
asked by Gerardo Díaz Rodríguez 04.04.2017 в 03:58
source

1 answer

2
  

run fine while the app runs normally, when I close the   application the service stops.

There are several things to comment:

-When the service is stopped is called onDestroy() , I recommend validating if the MediaPlayer instance is not null and also very important to call release() after stop() .

 @Override
    public void onDestroy() {
        super.onDestroy();
        if(med != null){
           med.stop();
           med.release();
        }
    }

-You do not need an instance of Timer .

- onStartCommand() is used to start the service not to instantiate MediaPlayer , for this onCreate() is used. Very important, the constant START_STICKY must have the value of 1 or use Service.START_STICKY :

  @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

       /* med= MediaPlayer.create(this, R.raw.uci);
        med.start();

        return START_STICKY;*/

        player.start();
        return Service.START_STICKY;
    }

On the other hand you comment that if you close the application the service stops, you must ensure that you are not calling:

 stopService(myService);

I add an example:

Service to play an audio stored in the / raw directory in the background.

package com.tototita.musicbackground;

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()");
    }
}
  

I close the app and stop playing but start again.

This is understandable, remember that Service.START_STICKY is being used, check the documentation:

  

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

     

Service.START_NOT_STICKY : The operating system will not recreate the service if the application is destroyed.

    
answered by 04.04.2017 / 17:57
source