In Android Application Development, cross-process communication is used, such as calling dialing, text messages, and emails in third-party applications, when data is transmitted in this process, the current application (one process) calls another application (the second process). This is cross-process communication in Android. In Android, there are four cross-process communication methods: Activity, BroadcastReceiver, ContentProvider, and AIDL (Android Interface Definition Language ), today, we will briefly describe the implementation of calling another process and passing data in one process through Activity.
Create a project named ProjectTwo, which is the project that receives messages from the first process. Then modify the Activity configuration in Mainifest. xml:
[Html]
<Activity
Android: name = ". ProjectTwoActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "com. ryan. test. MYACTION"/>
<Data android: scheme = "info"/>
<Category android: name = "android. intent. category. DEFAULT"/>
</Intent-filter>
</Activity>
The action tag specifies the action received by the Activity. The data tag defines the access protocol, and the category is DEFAULT by DEFAULT.
Then retrieve the data from the first project in the Activity:
[Java]
Public class ProjectTwoActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView valueTextView = (TextView) this. findViewById (R. id. TV _value );
// The getData method obtains the uploaded URI.
If (getIntent (). getData ()! = Null ){
// Obtain the data passed from the first process
String value = getIntent (). getExtras (). getString ("value ");
System. out. println ("----" + value );
ValueTextView. setText (value );
}
}
}
Create a project named ProjectOne and write the following code in the Activity:
[Java]
Public class ProjectOneActivity extends Activity {
// Call the Action of the Activity in another process
Private static final String ACTION = "com. ryan. test. MYACTION ";
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Button invokeButton = (Button) this. findViewById (R. id. btn_invoke );
InvokeButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// URI defines the communication protocol
Uri uri = Uri. parse ("info: // test ");
// Call the Activity in the second process through Action and URI and pass data
Intent invokeIntent = new Intent (ACTION, uri );
InvokeIntent. putExtra ("value", "Data from project one ");
StartActivity (invokeIntent );
}
});
}
}
Run ProjectTwo to the device, and then run ProjectOne. click the button to transfer data from process 1 (ProjectOne) to process 2 (ProjectTwo, the communication between two processes through Activity is completed. In addition, startActivity is used here, And the startActivityForResult method can also be used for value callback.
Running effect: