When using intent to start a activity,service, the data can be transmitted through the Putextra, and the triggered Activity,service can use Getintent Getextras () Extra, The corresponding parameter values are then based on the extra key value (key).
Roboguice provides a neat way to get these extra values through @injectextra tags.
This example uses two Activity,injectextrademo to trigger the injectextrareceiver, and when intent is created in Injectextrademo, two parameters are placed Putextra .
public class Injectextrademo extends
roboactivity {
@InjectView (R.id.button) button button;
@Inject context;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.injectextra);
Button.setonclicklistener (Mgolistener);
}
Private Onclicklistener Mgolistener = new Onclicklistener ()
{public
void OnClick (View v)
{
Intent DI = new Intent ();
Di.setclass (context, injectextrareceiver.class);
Di.addflags (Intent.flag_activity_new_task
| Intent.flag_activity_single_top);
Di.putextra ("Extra1", "Message1");
Di.putextra ("Extra2", "Message2");
Context.startactivity (DI);
}
;
}
In Injectextrareceiver through the @injectextra tag, Roboguice automatically injects the values passed by intent for these variables, optional=true indicates that the extra is optional, if the incoming Intent does not contain this extra, the value is null.
public class Injectextrareceiver extends roboactivity{
@InjectView (r.id.textview) TextView TextView;
@InjectExtra ("Extra1") String extra1;
@InjectExtra ("Extra2") String Extra2;
@InjectExtra (value= "Extra3", optional=true) String extra3;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.injectextrareceiver);
Textview.settext ("Extra1:" +extra1
+ "\r\nextra2:" +extra2
+ "\R\NEXTRA3:" +extra3);
}
This example downloads: Http://www.imobilebbs.com/download/android/roboguice/InjectExtraDemo.zi p
View a full set of articles: Http://www.bianceng.cn/OS/extra/201301/34950.htm