Android study notes-Intent (intent)

1. Introduction of Intent

(1) What is Intent

Intent is a runtime binding mechanism used to connect two different components during the running of the program. In short, Intent is a mechanism that assists in the interaction and communication between applications.

(B) Intent related attributes

Attributes description
component Destination component
action Act of expressing intent
category Type of performance
data Data to be manipulated with the action
type (data type) description of data
extras (extended information) Extended Information

Second, the actual use of Intent

(1) Display intention

Clearly indicate the component name.
1. Method One

//this指向当前的Activity,后面填写内容为需要跳转的Activity
intent.setClass(this,ClockActivity.class);

2. Method two

///this指向当前的Activity,后面是要跳转Activity的全类名
intent.setClassName(this,"com.example.intentthreetype.DateActivity");

3. Method Three

//前面内容是要打开Activity的项目名,后面是要跳转项目中Activity的全类名
intent.setClassName("com.example.helloworld","com.example.helloworld.MainActivity");

(2) Implicit intention

Do not directly specify the component name.
The intent-filter (intent filter)
adds an intent filter to the activity by setting the AndroidManifest.xml file, and sets the action.

<activity android:name=".Show_Image">
	<intent-filter>
	    <action android:name="openimage" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Three, examples

(1) Examples of display intentions

There are three ways to achieve access to another Activity
MainActivity code (only the more important part of the code is given)

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    
    private Button type1_btn;
    private Button type2_btn;
    private Button type3_btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        type1_btn=findViewById(R.id.type1_btn);
        type2_btn=findViewById(R.id.type2_btn);
        type3_btn=findViewById(R.id.type3_btn);
        type1_btn.setOnClickListener(this);
        type2_btn.setOnClickListener(this);
        type3_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
    
    
        Intent intent=new Intent();
        switch (view.getId())
        {
    
    
            case R.id.type1_btn:
                intent.setClass(this,ClockActivity.class);
                break;
            case R.id.type2_btn:
                intent.setClassName(this,"com.example.intentthreetype.DateActivity");
                break;
            case R.id.type3_btn:
                intent.setClassName("com.example.helloworld","com.example.helloworld.MainActivity");
                break;
        }
        startActivity(intent);
    }
}

Final display effect

(2) Examples of implicit intentions

Enter another Activity
MainActivity code through the button

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    
    Button btn_image;
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_image=findViewById(R.id.imageshow);
        btn_image.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
    
    
        Intent intent=new Intent();
        intent.setAction("openimage");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        startActivity(intent);
    }
}

AndroidManifest.xml code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.imageshow">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.ImageShow">
        <activity android:name=".Show_Image">
            <intent-filter>
                <action android:name="openimage" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

display effect

Nowadays, implicit intentions are mainly used in more ways, and this kind of use is more common. Supplementary explanation of the use of implicit intent. When there are multiple implicit intent definitions with the same name, there will be a choice when they are used. This is the part that is shared now. Usually when sharing content, multiple options will pop up, that is, several of them are given the same implicit action name.

Guess you like

Origin blog.csdn.net/qq_43279579/article/details/115242495