Intent has the following functions:
In mainactivity, add a listening event for button 1:
Listener1 = new onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Intent intent1 = new intent (mainactivity. This, activity1.class );
Intent1.putextra ("mainactivity", "This is data from mainactivity ");
Startactivityforresult (intent1,Request_code);
}
};
Receive data from intent in mainactivity in activity1:
String data = NULL;
Bundle extras =Getintent (). getextras ();
If (extras! = NULL ){
Data = extras. getstring ("mainactivity ");
}
Settitle ("now in activity1:" + data );
Add a listening event to the button in activity1, and an intent is returned:
Listener1 = new onclicklistener (){
@ Override
Public void onclick (view v ){
// Todo auto-generated method stub
Bundle bundle = new bundle ();
Bundle. putstring ("Store", "data from activity1 ");
Intent mintent = new intent ();
Mintent. putextras (bundle );
Setresult (Result_ OK, Mintent );
Finish ();
}
};
Override the onactivityresult () method in mainactivity to process the returned content:
@ Override
Protected void Onactivityresult (Int Requestcode , Int Resultcode , Intent data ){
// Todo auto-generated method stub
Super. onactivityresult (requestcode, resultcode, data );
If (requestcode =Request_code){
If (resultcode =Result_canceled){
Settitle ("cancel ");
} Else if (resultcode = Result_ OK ){
String temp = NULL;
Bundle extras = Data. getextras ();
If (extras! = NULL ){
Temp = extras. getstring ("Store ");
}
Settitle ("in mainactivity:" + temp );
}
}
}
Add a listener for button 2:
protected static final string Action1 = "com. sunny. action. broadcase ";
listener2 = new onclicklistener () {
@ override
Public void onclick (view V) {
// todo auto-generated method stub
intent intent2 = new intent (Action1);
sendbroadcast (intent2 );
}< BR >};
Add a broadcast receiver whose capture action is the intent of COM. Sunny. Action. broadcase to generate notification:
Public classBroadcastreceive1Extends broadcastreceiver {
Private Static final int icationication_id = 0;
Context context;
@ Override
Public voidOnreceive(Context, intent ){
// Todo auto-generated method stub
This. Context = context;
Shownotification ();
}
Private void shownotification (){
// Todo auto-generated method stub
Icationicationmanager notificationmanager = (notificationmanager) Context. getsystemservice (Android. content. Context. icationication_service );
Notification = new notification (R. drawable. Icon, "In broadcastreceive1", system. currenttimemillis ());
Pendingintent contentintent = pendingintent. getactivity (context, 0, new intent (context, mainactivity. Class), 0 );
Notification. setlatesteventinfo (context, "In broadcastreceive1:", null, contentintent );
Icationicationmanager. Notify (notification_id, notification );
}
}
It is registered in androidmanifest. xml:
<Cycler Android: Name = ". broadcastreceive1">
<Intent-filter>
<Action Android: Name = "com. Sunny. Action. broadcase"/>
</Intent-filter>
</Cycler>