The usage of Intent in Android development

Literally, Intent means intent. What you want to do, you can tell the Intent, it can help you do the work.

Intent is a running mechanism provided by Android, which can perform operations such as switching between different pages, transferring data, and calling external components. It can be understood as the role of a mediator between different components.

The attributes of Intent are Action (action), Data, Category (category), Type (type), Compent, Extra (extension) and so on. Among them, Action is the most commonly used.

constant meaning
Intent.ACTION_MAIN activity is the start of a program
Intent.ACTION_GET_CONTENT Users can select multimedia data such as pictures, files, etc.
Intent.ACTION_SEND send email
Intent.SMS_RECEIVED incoming mail
Intent.ACTION_AMSWER Handling incoming calls
Intent.ACTION_CALL_BUTTON Press the dial key
Intent.ACTION_CALL Call a specified phone number (direct dial)

The value transfer between activities can be done through intent.

Create an Intent with the following code.

Intent intent = new Intent(当前的activity.this,另一个activity.class);//此intent的目的是从当前的activity跳转到另一个activity。

Of course, there is another way, which is equivalent.

Intent intent = new Intent();
intent.setClass(当前的activity.this,另一个activity.class);

Use the following code to put data into the intent.

Here, we need to introduce a class Bundle, which means candied fruit in Chinese. The data you want to transfer is dressed in candied fruit in the form of key-value and thrown to another Activity.

Bundle bundle = new Bundle();
bundle.putString("key","value");
//存放数据,同时bundle还有其他方法,例如remove(String key)移除指定的key
//getString(String key) 获取指定key的值 等方法。
intent.putExtras(bundle);//数据对象坐上intent的车,intent帮忙给运过去

Then start another activity with the following code, and also pass the value in the past

startActivity(intent);//切换到另一个Activity

Another Activity gets the passed value in this way.

Bundle bundle = this.getIntent().getExtras();
String str = bundle.getString("key");

In addition, there are other ways to use intent, such as making calls.

Uri call= Uri.parse("tel:88888888");  
Intent intent = new Intent(Intent.ACTION_DIAL, call);  

above

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325477836&siteId=291194637