Hola @egoitz_gonzales, podrías usar la clase ResultReceiver
, la idea es enviar a tu intent una instancia de esta clase, despues usar el metodo send
en el metodo onHandleIntent
de tu servicio, cuando tu servicio haya finalizado.
clase que extiende ResultReceiver
:
public class MyReceiver extends ResultReceiver {
private Receiver mReceiver;
public MyReceiver(Handler handler) {
super(handler);
}
public interface Receiver {
public void onReceiveResult(int resultCode, Bundle resultData);
}
public void setReceiver(Receiver receiver) {
mReceiver = receiver;
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (mReceiver != null) {
mReceiver.onReceiveResult(resultCode, resultData);
}
}
}
en tu servicio:
@Override
protected void onHandleIntent(Intent intent)
{
// ...
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
Bundle bundle = new Bundle();
bundle.putStringArray("alguna cadena", "algun valor"); // etc
int STATUS_FINISHED = 1; // o como miembro de esta clase ...
receiver.send(STATUS_FINISHED, bundle);
}
en tu actividad podrias implementar la interface de tu clase ResultReceiver
public class MainActivity extends Activity implements MyReceiver.Receiver {
e implementa el metodo OnReceiveResult
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
// if( resultCode == MiServicio.STATUS_FINISHED ) ...
Fragment fragment = getFragmentManager().findFragmentById(R.id.mi_fragmento);
TextView textView = (TextView) fragment.getView().findViewById(R.id.algun_textview);
textView.setText("servicio finalizado");
}
comienza el servicio:
Intent msgIntent = new Intent(Main.this, CheckNuevosAvisosIntentService.class);
MyReceiver receiver = new MyReceiver(new Handler());
receiver.setReceiver(Main.this);
msgIntent.putExtra("iteraciones", 1);
msgIntent.putExtra("receiver", receiver);
startService(msgIntent);