Created
April 26, 2013 23:13
-
-
Save erichegt/5471041 to your computer and use it in GitHub Desktop.
Exemplo de uso do LocalBroadCastManager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PontosEOnibusTask extends AsyncTask<Coordenada, ArrayList<Ponto>> implements LongRunningTask{ | |
private BusaoApplication application; | |
public PontosEOnibusTask(BusaoApplication application) { | |
//... | |
} | |
//... | |
@Override | |
protected void onPostExecute(ArrayList<Ponto> result) { | |
if (TaskStatus.OK.equals(getEndBackgroundStatus())) { | |
PontosProximosEncontrados.notifica(application, result); | |
} else { | |
PontosProximosEncontrados.notificaFalha(application, mensagemDeFalha); | |
} | |
application.remove(this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FragmentA extends Fragment implements PontosEncontradosContextDelegate { | |
//... | |
private PontosProximosEncontrados pontosEncontrados; | |
@Override | |
protected void onCreateView(...) { | |
//... | |
pontosEncontrados = PontosProximosEncontrados.registraObservador(this); | |
//... | |
} | |
@Override | |
public void lidaComPontosProximos(ArrayList<Ponto> pontos) { | |
//aqui cada observador implementa o que quiser! | |
} | |
@Override | |
public void lidaComFalha(String mensagem) { | |
//aqui cada observador implementa o que quiser! | |
} | |
@Override | |
protected void onDestroyView() { | |
super.onDestroyView(); | |
pontosEncontrados.unregister(this); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Evento { | |
public void unregister(Context context); | |
} | |
public interface PontosEncontradosDelegate { | |
void lidaComPontosProximos(ArrayList<Ponto> pontos); | |
void lidaComFalha(String mensagem); | |
} | |
public interface PontosEncontradosContextDelegate extends PontosEncontradosDelegate{ | |
Context getContext(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PontosProximosEncontrados extends BroadcastReceiver implements Evento{ | |
private static final String FALHOU = "falhou"; | |
private static final String MENSAGEM_FALHA = "mensagemFalha"; | |
private static final String PONTOS = "pontos"; | |
private static final String PONTOS_ENCONTRADOS = "pontos-encontrados"; | |
private static final String PONTOS_NAO_ENCONTRADOS = "pontos-nao-encontrados"; | |
private PontosEncontradosDelegate delegate; | |
@Override | |
@SuppressWarnings("unchecked") | |
public void onReceive(Context context, Intent intent) { | |
if (intent.getBooleanExtra(FALHOU, false)) { | |
MyLog.i("RECEBIDA MENSAGEM DE falha! para delegate"+delegate); | |
delegate.lidaComFalha((String)intent.getSerializableExtra(MENSAGEM_FALHA)); | |
} else { | |
MyLog.i("RECEBIDA MENSAGEM DE SUCESSO! para delegate"+delegate); | |
ArrayList<Ponto> pontos = (ArrayList<Ponto>) intent.getSerializableExtra(PONTOS); | |
delegate.lidaComPontosProximos(pontos); | |
} | |
} | |
public static void notifica(Context context, ArrayList<Ponto> pontos) { | |
Intent intent = new Intent(PONTOS_ENCONTRADOS); | |
intent.putExtra(PONTOS, pontos); | |
LocalBroadcastManager.getInstance(context).sendBroadcast(intent); | |
} | |
public static Evento registraObservador(PontosEncontradosContextDelegate delegate){ | |
return registraObservador(delegate, delegate.getContext()); | |
} | |
public static Evento registraObservador(PontosEncontradosDelegate delegate, Context context){ | |
PontosProximosEncontrados receiver = new PontosProximosEncontrados(); | |
receiver.delegate = delegate; | |
LocalBroadcastManager.getInstance(context) | |
.registerReceiver(receiver, new IntentFilter(PONTOS_ENCONTRADOS)); | |
LocalBroadcastManager.getInstance(context) | |
.registerReceiver(receiver, new IntentFilter(PONTOS_NAO_ENCONTRADOS)); | |
return receiver; | |
} | |
public static void notificaFalha(Context context, String mensagem) { | |
Intent intent = new Intent(PONTOS_NAO_ENCONTRADOS); | |
intent.putExtra(MENSAGEM_FALHA, mensagem); | |
intent.putExtra(FALHOU, true); | |
LocalBroadcastManager.getInstance(context).sendBroadcast(intent); | |
} | |
public void unregister(Context context) { | |
LocalBroadcastManager.getInstance(context).unregisterReceiver(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment