Android Intent, broadcast

Source: Internet
Author: User

The intent features are:

To add a listen event for Button 1 in mainactivity:

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 listen event for the button in Activity1 and return a intent:

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 ();
}
};

Overwrite the Onactivityresult () method in mainactivity, processing the returned content:

@Override
protected voidOnactivityresult(intRequestcodeIntResultCode, 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);
}
}
}

To add a listener event 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);
}
};

Add a broadcast Receiver that captures the intent of the action Com.sunny.action.BROADCASE, generating notification:

Public class  broadcastReceive1  extends broadcastreceiver {
    private static final int notification_id = 0;
    Context context;
    
    @Override
    public void  OnReceive (Context context, Intent Intent) {
       //TODO auto-generated method Stub
        this.context=context;
        shownotification ();
   }

private void Shownotification () {
TODO auto-generated Method Stub
Notificationmanager notificationmanager= (Notificationmanager) Context.getsystemservice ( Android.content.Context.NOTIFICATION_SERVICE);
Notification 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);
Notificationmanager.notify (notification_id, NOTIFICATION);
}

}

It is registered in Androidmanifest.xml:

<receiver android:name= ". broadcastReceive1" >
<intent-filter>
<action android:name= "Com.sunny.action.BROADCASE"/>
</intent-filter>
</receiver>

The intent features are:

To add a listen event for Button 1 in mainactivity:

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 listen event for the button in Activity1 and return a intent:

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 ();
}
};

Overwrite the Onactivityresult () method in mainactivity, processing the returned content:

@Override
protected voidOnactivityresult(intRequestcodeIntResultCode, 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);
}
}
}

To add a listener event 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);
}
};

Add a broadcast Receiver that captures the intent of the action Com.sunny.action.BROADCASE, generating notification:

public class BroadcastReceive1 extends Broadcastreceiver {
private static final int notification_id = 0;
Context context;

@Override
public void OnReceive(context context, Intent Intent) {
TODO auto-generated Method Stub
This.context=context;
shownotification ();
}

private void Shownotification () {
TODO auto-generated Method Stub
Notificationmanager notificationmanager= (Notificationmanager) Context.getsystemservice ( Android.content.Context.NOTIFICATION_SERVICE);
Notification 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);
Notificationmanager.notify (notification_id, NOTIFICATION);
}

}

It is registered in Androidmanifest.xml:

<receiver android:name= ". broadcastReceive1" >
<intent-filter>
<action android:name= "Com.sunny.action.BROADCASE"/>
</intent-filter>
</receiver>

Android Intent, broadcast

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.