BroadcastReceiver broadcast receiver (3)-transmit data between applications using Broadcast
??
The TestBroadcastA application is as follows:
MainActivity is as follows:
Package cc. testbroadcasta; import android. OS. bundle; import android. app. activity;/*** Demo Description: * use broadcast to transmit data between applications ** register a broadcast receiver in the TestBroadcastA application. * The TestBroadcastB application sends out a broadcast */public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main );}}
BroadcastReceiverTest is as follows:
Package cc. testbroadcasta; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; public class BroadcastReceiverTest extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {String name = intent. getStringExtra (name); int number = intent. getIntExtra (number, 0); System. out. println (received broadcast name = + name +, number = + number);} public static void main (String [] args ){}}
Main. xml is as follows:
AndroidManifest. xml is as follows:
The TestBroadcastB application is as follows:
MainActivity is as follows:
Package cc. testbroadcastb; import android. OS. bundle; import android. app. activity; import android. content. intent; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Intent intent = new Intent (); // sets Actionintent. setAction (cc. test); // carries data intent. putExtra (name, Stephen Chow); intent. putExtra (number, 9527); // send broadcast sendBroadcast (intent );}}