Pregunta

Este servicio intensivo que creé mostrará tostadas en OnStartCommand () y en OnDestroy (), pero no en OnHandleIntent (). ¿Me estoy perdiendo algo sobre las limitaciones de un servicio intensivo?

public class MyService extends IntentService {

private static final String TAG = "MyService";

public MyService(){
    super("MyService");
}

@Override
protected void onHandleIntent(Intent intent) {
    cycle();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); //This happens!
    return super.onStartCommand(intent,flags,startId);
}

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

}

@Override
public void onDestroy() {
    Toast.makeText(this, "service stopping", Toast.LENGTH_SHORT).show(); //This happens!
    super.onDestroy();
}

private void cycle(){
      Toast.makeText(this, "cycle done", Toast.LENGTH_SHORT).show();  //This DOESN'T happen!
      Log.d(TAG,"cycle completed"); //This happens!
}
}
¿Fue útil?

Solución

OnHandleIntent () se llama desde un hilo de fondo (de eso se trata IntentService), por lo que no debe hacer UI desde allí.

Otros consejos

La respuesta aceptada no es correcta.

Así es como puedes mostrar tostadas de onHandleIntent():

Crea una clase DISPOSTOTTOAST:

public class DisplayToast implements Runnable {
    private final Context mContext;
    String mText;

    public DisplayToast(Context mContext, String text){
        this.mContext = mContext;
        mText = text;
    }

    public void run(){
        Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
    }
}

Instanciar un Handler en el constructor de su servicio y llame al post método con un DisplayToast objeto dentro.

public class MyService extends IntentService {
Handler mHandler;

public MyService(){
    super("MyService");
    mHandler = new Handler();
}

@Override
protected void onHandleIntent(Intent intent) {
    mHandler.post(new DisplayToast(this, "Hello World!"));

}
}

Debe comenzar la tostada en el hilo principal:

new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
        }
});

Esto se debe a que de lo contrario el hilo del servicio de la intensidad deja antes de que se pueda enviar la tostada, causando una explicación ilegal:

java.lang.illegalstateException: handler (android.os.handler) {12345678} Envío de mensaje a un controlador en un hilo muerto

Otra opción es Rxjava, p.ej:

private void showToast(final String text) {
    Observable.just(text)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
            }
        });
}

Advertencia: Soy nuevo en Android.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top