Saturday, February 11, 2012

How to set up a BroadcastReceiver intent in Android?


This code will send a Broadcast with a message, from a service to an activity.
It is important to remember to register the reciever, so the Broadcast is picked up.
public class service {
//This code will send the broadcast
public static final String DB_INTENT = "com.infosol.infoburst.LogViewer.broadcast.DB_UPDATE";

private void SendBroadcast(Boolean result, String Msg){
Intent i = new Intent();
i.setAction(DB_INTENT);
i.putExtra("result", result);
i.putExtra("message", Msg);
this.sendBroadcast(i);
}
}


public class activity{

//This needs to be in the activity that will end up receiving the broadcast
registerReceiver(receiver, new IntentFilter("com.infosol.infoburst.LogViewer.broadcast.DB_UPDATE"));

//This will handle the broadcast
public BroadcastReceiver receiver = new BroadcastReceiver() {
//@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(LogService.DB_INTENT)) {
               }
}
};
}

No comments:

Post a Comment