Android cross-process communication-Activity

Source: Internet
Author: User

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:

 


Related Article

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.