Interacting with other Apps

Source: Internet
Author: User

Http://www.cnblogs.com/gcg0036/p/4321279.html

Build An implicit Intent:

Implicit intent does not declare the specific name of the component of the class to invoke, only declares the action to be performed

Usually also contains data related to action

1. If your data is a URI, you can use a very simple intent () constructor to define the action and data

Uri number = Uri.parse ("tel:5551234"new Intent (intent.action_dial, number);

When your app refers to the above intent by calling StartActivity (), the dial-up app dials the call above.

2. Other implicit intent may require "extra" data to provide different data types, such as a string type, and we can add one or several additional data using a number of overloaded Putextra ()

By default: The system chooses the appropriate MIME type for intent based on the included URI data. If intent does not contain a URI, you need to use Settype () to specify the data type that the intent is associated with. Determining the MIME type further determines which activity can receive the intent.

New= Calendar.getinstance (). Set (2012, 0, 19, 10, 0, 7, + = Calendar.getinstance (). "Ninja class" "Secret Dojo");

Note: the more specific the definition of intent, the better, if you want to browse the picture, through Action_view intent, you will need to specify the MIME type of image/*, which will prevent the app to access other types of data (such as the map app) Inspired by this intent. Since you have specified a MIME type, other apps will not be trigger by this intent because of inconsistent data types.

Although the system platform will map specific intent to the built-in app, it is a good idea to verify that it is available before referencing a intent.

Because if you cite a intent that no app can respond to, the consequences are serious and the app crashes

Packagemanager Packagemanager = Getpackagemanager (); List<ResolveInfo> activities = packagemanager.queryintentactivities (Intent, 0); boolean isintentsafe = activities.size () > 0;

Note: When your activity starts for the first time, you need to do this check so that you can block some features because these features do not have the activity to respond to the intent of these features. This will avoid the crash of the app. If you know what app can handle this intent you can't handle in your app, you can also provide a download link to Google Play.

Start an Activitywith the Intent:

Build good one intent, and after entering the relevant information, you can call the StartActivity () method, you can send this intent to the system. If the system discovers more than one app can handle this intent, the system will provide a list to let you choose, if there is only one, there is no choice, then directly launched.

The following code is a complete code to view the map: including the new intent, verifying that there is handle its activity, startup, etc.

//Build The IntentUri location = Uri.parse ("Geo:0,0?q=1600+amphitheatre+parkway,+mountain+view,+california"); Intent mapintent=NewIntent (Intent.action_view, location);//Verify it resolvesPackagemanager Packagemanager =Getpackagemanager (); List<ResolveInfo> activities = packagemanager.queryintentactivities (mapintent, 0);BooleanIsintentsafe = activities.size () > 0; //Start an activity if it s safeif(Isintentsafe) {startactivity (mapintent);}

Getting a Resultfrom an Activity:

Note: When you call the Startactivityforresult () method, both explicit and hidden intent can be used, but the activity that is used to accept other activity returns results must use explicit intent, To ensure that it receives.

The following code selects the activity of a contact and returns a touch:

Static Final int Pick_contact_request = 1;  // The request Code ... Private void pickcontact () {    newnew Uri ("content://contacts"));     // Show user only contacts W/phone numbers     Startactivityforresult (pickcontactintent, pick_contact_request);}

The following code receives the returned result:

@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    //Check which request we ' re responding to    if(Requestcode = =pick_contact_request) {        //Make sure the request was successful        if(ResultCode = =RESULT_OK) {            //The user picked a contact. //The Intent ' s data Uri identifies which contact is selected. //Do something with the contact here (bigger example below)}

For example, the Contact app returns the contact information for the selected contacts URI as a result, the camera returns a bitmap as the result.

Handle the Intent in Your Activity

@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.main); //Get The intent that started this activityIntent Intent =getintent (); Uri Data=Intent.getdata (); //Figure out how to does based on the intent type    if(Intent.gettype (). IndexOf ("image/")! =-1) {        //Handle intents with image data ...}Else if(Intent.gettype (). Equals ("Text/plain")) {        //Handle intents with text ...    }}

If you want your activity to return a value, you only need to call the Setresult () method to specify result code and result intent. When the user in this activity is completed, it is necessary to return to the original activity, call the finish () method can close the activity, back to the original.

// Create Intent to deliver some kind of result data // two parameters: 1.result code is ACTIVITY.RESULT_OK  2. Result intent  Default value is result_cancelednew Intent ("Com.example.RESULT_ACTION", Uri.parse (" Content://result_uri "); Setresult (ACTIVITY.RESULT_OK, result); finish ();

There is no need to test which method your activity is starting with, and whether the return value is the same, use Setresult () to set the return value, if the original activity uses a method that needs to return a value startactivityforresult () to call your Activity, it is returned to it according to the set return value, if you call your activity with startactivity (), then you simply ignore the return value of the original set if the return value is not set.

Http://blog.sina.com.cn/s/blog_8191005601019yci.html

Interacting with other Apps

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.