Several basic methods of using Intent in Android studio

 In Android development, Intent is the most basic and commonly used operation. Intent operations are also required in core components such as Activity, Service, and BroadcastReceiver. Below we will introduce in detail some basic usage of Intent in development.

Assume that there are currently two activities, FirstActivity and SecondActivity , where FirstActivity is the initial activity to start the activity.

SecondActivity is the activated activity

Display Intent (directly specify the target component, which is an exact match)

(1) Implemented in the construction method

//创建一个目标意图,this为当前活动FirstActivity
Intent intent=new Intent(this,SecondActivity.class);

//调用startActivity方法启动SecondActivity
startActivity(intent);

(2) Call the setClass method

//创建一个意图
Intent intent=new Intent();

//调用setClass方法设置要跳转的意图
intend.setClass(this,SecondActivity.class);

//调用startActivity方法启动SecondActivity
startActivity(intent);

(3) Call the setComponent method

//创建一个意图
Intent intent=new Intent();

//创建一个包含目标活动的ComponentName组件
ComponentName component = new ComponentName(this, SecondActivity.class);

//调用setComponent方法将封装信息传给意图对象intent
intent.setComponent(component);

//调用startActivity方法启动SecondActivity
startActivity(intent);

Implicit Intent (does not directly specify the target component that needs to be activated, and is a fuzzy match)

 Different from explicit Intent, implicit Intent does not need to specify a specific activity name and is matched through actions (action)

Find the code snippet of .SecondActivity in the manifest file (AndroidManifest.xml)

Set the action attribute as follows

Set the category attribute as follows

Note: There can be multiple action attributes and category attributes, just match any one.

<activity android:name=".SecondActivity">
    <intent-filter> 
            <!--设置action属性-->
        <action android:name="android.intent.action.Second" /> 
            <!--设置category属性-->
        <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity>

After setting the action attributes, you can use the setAction method in the code to transfer and set the corresponding activity action. The code is as follows

//创建一个意图对象
Intent intent=new Intent();
//设置action动作
intent.setAction("android.intent.action.Second");
//调用startActivity方法启动SecondActivity
startActivity(intent);

In fact, Android development also provides some commonly used actions for implicitly calling system applications.

Instantiate an Intent object

Intent intent=new Intent();

sending a text message

intent.setAction(Intent.ACTION_SENDTO);
//或者
intent.setAction("android.intent.action.SENDTO");

ready to dial 

intent.setAction(Intent.ACITON_DIAL);
//或者
intent.setAction("android.intent.action.DIAL");

 Send content

intent.setAction(Intent.ACTION_SEND);
//或者
intent.setAction("android.intent.action.SEND");

The above are some basic usages in Android development (record the learning process)

If you find it useful, please give it a like and leave~

Guess you like

Origin blog.csdn.net/Lic_Ac/article/details/127232170