Android Development (26) and other applications exchange data method 1. Use intent to specify a custom action to call activ in other programs

Source: Internet
Author: User

We will encounter interactions with other applications during development. The following is a simple method. The overall usage is similar to "using intent to call the built-in photo app and get the results ".

First look at the page:

 

Let's take a look at the implementation steps.

The first application DEMO1:

1. Create a custom action and send it using intent

   String action = "zyf.demo.customAction";   Intent n = new Intent(action);      

2. Add custom content in the attachment, and use putExtra

N. putExtra ("key1", "hello, this is the message from demo1. ");

3. check whether there is content matching this action first.

String action = "zyf. demo. customAction "= Intent intent = <ResolveInfo> resolveInfo = (resolveInfo. size ()> 0 "matching activity found", 0 "no matching activity found", 0

 

4. Use startActivityForResult to start Intent.

StartActivityForResult (n, REQUEST_CODE_1 );

5. Get the result (from DEMO2)

Final int REQUEST_CODE_1 = 1; @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = REQUEST_CODE_1) {String res = data. getStringExtra ("result"); Toast. makeText (getBaseContext (), "Received:" + res, 0 ). show ();} elsesuper. onActivityResult (requestCode, resultCode, data );}

  

The second application, DEMO2:

1. Create an activity to receive the action specified above and register intent-filter in the AndroidManifest. xml file.

      <="zyf.demo.demo2.MainActivity"="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>                   </activity>

2. Data from DEMO1 can be displayed in the activity.

     == intent.getStringExtra("key1"

3. process the returned content when the current application is disabled in the activity.

= "Result", "this is the result from demo2"

 

This is done. DEMO code download

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.