# Intent

## Intent是什么？

Intent一般作为参数来使用，协助完成 Android各个组件之间的通讯。

Intent主要包括7个属性：**Action（动作）**、**Data（数据）**、**Category（类别）**、**Type（数据类型）**、**Component（组件）**、**Extra（扩展信息）**、**Flag（标志位）**。其中最常用的是Action和Data。

## 表现形式：

* 启动Activity

  ```java
  startActivity(intent);
  startActivityForResult(intent, requestCode);
  ```
* 启动Service

  ```java
  startService(intent);
  ```
* 发送Broadcast

  ```java
  sendBroadcast(intent);
  sendOrderedBroadcast(intent, sendOrderedBroadcast);
  ```

## Intent种类

### 显式Intent

```java
//最常用方法
Intent intent = new Intent(this, SecondActivity.class);  
startActivity(intent);  

//setClass 或者 setClassName
Intent intent = new Intent();    
intent.setClass(this, SecondActivity.class);  
//或者intent.setClassName(this, "com.example.app.SecondActivity");  
//或者intent.setClassName(this.getPackageName(),"com.example.app.SecondActivity");            
startActivity(intent);  

//setComponent
Intent intent = new Intent();    
intent.setComponent(new ComponentName(getPackageName(), SecondActivity.class))
startActivity(intent);  
```

### 隐式Intent

隐式，不明确指定启动哪个Activity，而是设置Action、Data、Category，让系统来筛选出合适的Activity。

```java
//打电话
Uri uri = Uri.parse("tel:10010");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);

//使用选择器
Intent sendIntent = new Intent(Intent.ACTION_SEND);
Intent chooser = Intent.createChooser(sendIntent, title);
// Verify the original intent will resolve to at least one activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xayvytr.gitbook.io/android/intent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
