Let's say that I've the following main activity:
public class MwConsoleActivity extends Activity {
private classChild child = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
child = new classChild();
}
}
Then consider the implementation of the class "classChild":
public class MwBondingAgent extends SapereAgent {
MwBondingAgent(){}
public void AddEventListener(childAddedEvent event) {
//Send the data of event back to the main activity
}
}
I've tried to use IntentServices but was not able to receive the values back to the main activity. What would be the approach I've to take?
Cheers
Ali
解决方案
You can use and intentFilter to listen for broadcasts.
Add this to the activity:
IntentFilter intentFilter = new IntentFilter(
"com.unique.name");
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//extract our message from intent
String msg_for_me = intent.getStringExtra("some_msg");
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
In your class add this to the part you want to notify the activity:
Intent i = new Intent("com.unique.name").putExtra("some_msg", "I have been updated!");
this.sendBroadcast(i);