Android intent and broadcast

Source: Internet
Author: User

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>

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.