Interactive core Intent usage analysis between Android components _android

Source: Internet
Author: User

This example describes the interactive core intent usage between Android components. Share to everyone for your reference, specific as follows:

Starting from one activity to another can use the StartActivity () method or the Startactivityforresult () method

First: Start an activity directly

Intent Intent = new Intent (main.this, secondactivity.class);
StartActivity (Intent);

Second: Start another activity and return the result

Function: When you jump back from the second activity to the previous one, you no longer need to use the startactivity, which means you don't have to use the StartActivity method two times

Startactivityforresult (Intent Intent, Int requestcode)

Intent the data and actions passed to the activity to be jumped

Requestcode >=0 is good, casually used in the Onactivityresult () to distinguish which module return data, if there are C.java, D, or even e-module, each section is divided into different requestcode good.

The public class Mainactivity extends activity {/** called the ' when ' is the ' The activity ' is the ' the './@Override Public
      void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
      Setcontentview (R.layout.main);
      Add the Binding event button MyButton = (button) Findviewbyid to the button (R.id.mybutton);
      Mybutton.settext ("My first button");
    Mybutton.setonclicklistener (New OnClick ()); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {SYSTEM.OUT.PRINTLN (re
    Questcode);
  System.out.println (ResultCode); The public class OnClick implements onclicklistener{@Override the public void onclick (View v) {//Generate an in
        Tent object Intent Intent = new Intent ();
        Intent.putextra ("Testintent", "Testextra");
        Intent.setclass (Mainactivity.this,secondactivity.class);
        Direct activation of an activity//startactivity (intent); Initiates an activity Startactivityforresult with a return value (intent, 2);
      }
    }
}

 

Setresut (int resultcode, Intent Intent)

ResultCode If the activity sub module of the jump may have several different results to return, this parameter can be used to identify the distinction. There is also a special RESULT_OK value, there is no special case to use it, the SDK has instructions.

Intent continue not to explain, return to a onactivityresult ()

Copy Code code as follows:
Onactivityresult (int requestcode, int resultcode, Intent Intent)

There are three of them that need not be explained, with the corresponding things above. If no distinction is made between Requestcode and ResultCode, as long as there are other activity setresult to a onactivityresult () there will be no difference

public class Secondactivity extends activity{
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    //TODO auto-generated method Stub
    super.oncreate (Savedinstancestate);
    This.setcontentview (r.layout.other);
    Button btn = (button) Findviewbyid (R.id.otherbutton);
    Btn.setonclicklistener (New Button.onclicklistener () {
      @Override public
      void OnClick (View v) {
        Intent Intent = Getintent ();
        Setresult (3, intent);
        Finish ();}}
    );
  }


How do I use a custom action property?

1. Define a custom action name--constant

public static final String my_action = "Hb.com.MY_ACTION";

2, use a button and then give it the binding event, let it jump to another activity

Myactionbtn.setonclicklistener (New Button.onclicklistener () {
  @Override public
  void OnClick (View v) {
    System.out.println ("myactionbtn");         Intent Intent = Getintent ();
Here must be new a Intent object, if the above is opened is the current activity
    Intent Intent = new Intent ();
    Intent.setaction (my_action);
    StartActivity (intent);
  }
);

3, add the name of the activity in the Androidmanifest.xml configuration file

<activity android:name= ". Secondactivity "android:label=" @string/secondactivity ">
  <intent-filter>
    <action android: Name= "Hb.com.MY_ACTION"/>
    <category android:name= "Android.intent.category.DEFAULT"/>
  </ Intent-filter>
</activity>

Note: Hb.com.MY_ACTION This value is the same as intent.setaction (my_action); The value is the same
activity label inside the application tag
Category This property must appear in intent filter, or you cannot test that the
Intent Data property is the URI and MIME type of the specified action, and that different actions have different data designations
The Category property in intent is an additional information that performs the action
Intent Extras property is additional information for adding some components

Show _id 1 Contact information data = "CONTENT://CONTACTS/PEOPLE/1";
URI = uri.parse (data);
Action_view display data to the user intent.setaction (Intent.action_view);
Intent.setdata (URI);
StartActivity (Intent);
Edit _id 1 Contact information data = "CONTENT://CONTACTS/PEOPLE/1";
URI = uri.parse (data);
Action_edit Display editable Data intent.setaction (Intent.action_edit);
Intent.setdata (URI);
StartActivity (Intent);
Show the interface of the call to data = "tel:13811111111";
URI = uri.parse (data);
Action_dial Displays the panel intent.setaction (intent.action_dial) where the phone is dialed;
Intent.setdata (URI);
StartActivity (Intent);
Direct call data = "tel:13811111111";
URI = uri.parse (data);
Action_call directly call Intent.setaction (Intent.action_call);
Intent.setdata (URI);
StartActivity (Intent);
Access browser data = "http://www.baidu.com";
URI = uri.parse (data);
Action_view display data to the user intent.setaction (Intent.action_view);
Intent.setdata (URI);
StartActivity (Intent);
Access map data = "geo://39.92,116.46";
URI = uri.parse (data);
Action_view display data to the user Intent = new Intent (Intent.action_view,uri);

StartActivity (Intent); 

The action in the <intent-filter> list cannot be empty or the program block cannot be passed.

If the intent object specifies an action attribute, you cannot pass the test if you want to pass the action test, the property specified by the intent object must match the intent filter.

If the Action property is not specified in the intent object, the test is passed automatically.

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.