[Android]-- Intent (explicit and implicit Intent)

What are Intents?

Intent is a bridge for information communication between various components. It is used for communication between Android components and mainly completes the following tasks:

  • Indicate where the communication request comes from, where it goes, and how to go.
  • The initiator carries the data content required for this communication, and the receiver parses the data from the received intent.
  • If the initiator wants to judge the processing result of the receiver, the intent is responsible for letting the receiver send back the data content of the response.

 Components of Intent


1. Explicit Intent and Implicit Intent

1. Explicit Intent

Explicit Intent, which directly specifies the source activity and the target activity, is an exact match, and there are three construction methods:

  • Specified in the constructor of Intent.
  • Call the setClass method of the intent object to specify.
  • Call the setComponent method of the intent object to specify.

 (1) Specify in the Intent constructor

example:

Intent intent = new Intent(this,ActNextActivity.class)//创建一个目标确定的意图

(2) Call the setClass method of the intent object to specify

example:

Intent intent = new Intent();//创建新意图
intent.setClass(this,ActNextActivity.class)//设置意图要跳转的目标活动

(3) Call the setComponent method of the intent object to specify

example:

Intent intent = new Intent();//创建新意图
//创建包含目标活动在内的组件名称对象
ComponentName component = new ComponentName(this,ActNextActivity.class);
intent.setComponent(component);//设置意图携带的组件信息

2. Implicit Intent

The target activity to jump to is not clearly specified, but only an action string is given for the system to automatically match, which is a fuzzy match.

Usually the APP does not want to expose the activity name to the outside, but only gives a pre-defined tag string. This action name tag string can be an action defined by itself or an existing system action. Common system action values ​​are as follows:

 example:

java

public class ActionUrlActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_action_url);
        findViewById(R.id.btn_dial).setOnClickListener(this);
        findViewById(R.id.btn_sms).setOnClickListener(this);
        findViewById(R.id.btn_my).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String phoneNo = "12345";
        Intent intent = new Intent();
        switch (view.getId()){
            case R.id.btn_dial:
                //设置意图动作为准备拨号
                intent.setAction(Intent.ACTION_DIAL);
                Uri uri = Uri.parse("tel:"+phoneNo);
                intent.setData(uri);
                startActivity(intent);
                break;
            case R.id.btn_sms:
                //设置意图动作为发短信
                intent.setAction(Intent.ACTION_SENDTO);
                Uri uri2 = Uri.parse("smsto:"+phoneNo);
                intent.setData(uri2);
                startActivity(intent);
                break;
            case R.id.btn_my:
                intent.setAction("android.intent.action.NING");
                intent.addCategory(Intent.CATEGORY_DEFAULT);
                startActivity(intent);
                break;
        }
    }
}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="点击以下按钮向号码发起请求"/>

    <Button
        android:id="@+id/btn_dial"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到拨号页面"/>

    <Button
        android:id="@+id/btn_sms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到短信页面"/>

    <Button
        android:id="@+id/btn_my"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳到我的页面"/>

</LinearLayout>

The AndroidManifest.xml file of the custom page that needs to be redirected to

        <activity
            android:name=".ButtonClickActivity"
            android:exported="true">//需要设置为true,意为允许其他应用跳转
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            //添加的代码:
            <intent-filter>
                <action android:name="android.intent.action.NING" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>

 


Guess you like

Origin blog.csdn.net/Tir_zhang/article/details/126937291