There are several ways to update the View
of a Activity
of an external class, I will illustrate two that are easy to implement:
1. Using Design Pattern Observer
( Listener
)
Definition of Listener Interface
public interface OnEventChangedListener {
public void onEventChanged(/* Parametros */);
}
This interface will be used by BroadcastReceiver
and implemented by Activity
to handle the occurrence of the event.
Definition of BroadcastReceiver
If you are registering it in its AndroidManifest
and not creating and registering programmatically in its Activity
, the OnEventChangedListener
must be static
, otherwise it can be an instance variable and with a setter not static.
public class MyBroadcastReceiver extends BroadcastReceiver {
static OnEventChangedListener mListener;
@Override
public void onReceive(Context context, Intent intent) {
if(mListener != null) {
mListener.onConnectionChanged(/* Parametros */);
}
}
public static void setOnOnEventChangedListener(OnEventChangedListener listener) {
mListener = listener;
}
}
public class MyBroadcastReceiver extends BroadcastReceiver {
OnEventChangedListener mListener;
@Override
public void onReceive(Context context, Intent intent) {
if(mListener != null) {
mListener.onConnectionChanged(/* Parametros */);
}
}
public void setOnOnEventChangedListener(OnEventChangedListener listener) {
mListener = listener;
}
}
In your Activity
, you only need to register Listener
:
Definition of Activity
public class MainActivity extends Activity implements OnEventChangedListener {
@Override
public void onDestroy() {
// Remover a referência para não causar um Leak de memória!
MyBroadcastReceiver.setOnOnEventChangedListener(null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Restante do código de sua Activity
// Registro do Listener
MyBroadcastReceiver.setOnOnEventChangedListener(this);
}
@Override
public void onPause() {
super.onPause();
// É legal tirar a referência quando a Activity sair do topo do TaskStack.
MyBroadcastReceiver.setOnOnEventChangedListener(null);
}
@Override
public void onResume() {
super.onResume();
// Recolocando quando ela vier a ser o topo do TaskStack.
MyBroadcastReceiver.setOnOnEventChangedListener(this);
}
@Override
public void onEventChanged(/* Parametros */) {
// Tratamento do evento
}
}
If you create and register BroadcastReceiver
in your Activity
:
public class MainActivity extends Activity implements OnEventChangedListener {
MyBroadcastReceiver mReceiver;
IntentFilter mItentFilter;
@Override
public void onDestroy() {
// Remover a referência para não causar um Leak de memória!
mReceiver.setOnOnEventChangedListener(null);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Registro do BroadcastReceiver
mReceiver = new MyBroadcastReceiver();
mReceiver.setOnOnEventChangedListener(this);
mItentFilter = new IntentFilter();
mItentFilter.addAction("SEU_ACTION");
// Espera para registrar no onResume
//registerReceiver(mReceiver, mItentFilter);
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
// É legal tirar a referência quando a Activity sair do topo do TaskStack.
MyBroadcastReceiver.setOnOnEventChangedListener(null);
}
@Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, mItentFilter);
// Recolocando quando ela vier a ser o topo do TaskStack.
MyBroadcastReceiver.setOnOnEventChangedListener(this);
}
}
2, Using Handler
In this case, we will create a Handler
in Main Thread
, which will receive messages from BroadcastReceiver
, updating View
.
Definition of Activity
public class MainActivity extends Activity implements OnEventChangedListener {
Handler mHandler;
MyBroadcastReceiver mReceiver;
IntentFilter mItentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what == CONSTANTE_ATUALIZAR_VIEW) {
Bundle data = msg.getData();
// Mensagem enviada pelo BroadcastReceiver.
// Será executada na MainThread.
// Atualizar a View com os parametros.
}
}
};
// Registro do BroadcastReceiver
mReceiver = new MyBroadcastReceiver();
mReceiver.setHandler(mHandler);
mItentFilter = new IntentFilter();
mItentFilter.addAction("SEU_ACTION");
// Espera para registrar no onResume
//registerReceiver(receiver, mItentFilter);
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(mReceiver);
}
@Override
public void onResume() {
super.onResume();
registerReceiver(mReceiver, mItentFilter);
}
}
As reminded by Piovezan , we left to register in onResume
of Activity
, and we removed the registry in% with%. To avoid consuming unnecessary resources while onPause
is not visible. Of course it may be a decision not to do, it may be urgent to treat the event even with Activity
not visible.
Definition of Activity
public class MyBroadcastReceiver extends BroadcastReceiver {
Handler mHandler;
@Override
public void onReceive(Context context, Intent intent) {
if(mHandler != null) {
Message msg = mHandler.obtainMessage(CONSTANTE_ATUALIZAR_VIEW);
Bundle data = new Bundle();
// Seta os parametros no Bundle
msg.setData(data);
// Envio a mensagem para o Handler na MainActivity
mHandler.sendMessage(msg);
}
}
public void setHandler(Handler handler) {
mHandler = handler;
}
}
The process for BroadcastReceiver
registered in BroadcastReceiver
is analogous.
References:
link