BroadcastReceiver broadcast receiver (III)-transmit data between applications using broadcast, broadcastreceiver
Zookeeper
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:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "This Is A BroadcastReceiver. \ n receives broadcasts from another application. "android: layout_centerInParent =" true "/> </RelativeLayout>
AndroidManifest. xml is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cc. testbroadcasta "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 10 "android: targetSdkVersion = "10"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <activity android: name = "cc. testbroadca Sta. mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <! -- Register a broadcast receiver --> <receiver er android: name = "cc. testbroadcasta. broadcastReceiverTest "> <intent-filter> <action android: name =" cc. test "/> </intent-filter> </receiver> </application> </manifest>
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 the data intent. putExtra ("name", "Stephen Chow"); intent. putExtra ("number", 9527); // send broadcast sendBroadcast (intent );}}
Main. xml is as follows:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "a broadcast \ n is sent and transmitted to another application using broadcast." android: layout_centerInParent = "true"/> </RelativeLayout>
In Android, how does the Activity class transmit data to the BroadcastReceiver class?
Send broadcast:
Private void sendCurrentBoadcast (){
Intent intent = new Intent ();
Intent. setAction (BROADCAST_ACTION );
// Transmit data through the Intent object
Intent. putExtra ("msg", "welcome to join the android development team! ");
SendBroadcast (intent );
}
Data transmission between different applications
1. Values are transmitted from the main form to the subform. The first is to provide a reload constructor in the subform to transmit values using the reload constructor. This is suitable for a small number of values. The second is, define a main form object in the subform, and then you can receive the attribute values of the main form. This applies to the large number of values. 2. From the return value of the subform to the main form, the subform attribute is used to save the value of the subform. In the main form, the subform attribute can be accessed. Because there is no time, the answer is simple, I hope you can understand that the answers to many questions can be found in csdn. We recommend that you use csdn more. Recommended articles: