安卓开发intent传输
时间: 2023-10-04 10:14:05 浏览: 49
在 Android 中,Intent 是一种用于在应用程序之间传递信息的机制。可以通过 Intent 传递数据、启动活动、服务和广播等。以下是一些传递数据的示例:
1. 传递字符串:
```java
// 创建Intent对象
Intent intent = new Intent(this, YourActivity.class);
// 添加数据
intent.putExtra("key", "value");
// 启动Activity
startActivity(intent);
```
在目标活动中获取数据:
```java
// 获取Intent对象
Intent intent = getIntent();
// 获取数据
String value = intent.getStringExtra("key");
```
2. 传递整数:
```java
// 创建Intent对象
Intent intent = new Intent(this, YourActivity.class);
// 添加数据
intent.putExtra("key", 123);
// 启动Activity
startActivity(intent);
```
在目标活动中获取数据:
```java
// 获取Intent对象
Intent intent = getIntent();
// 获取数据
int value = intent.getIntExtra("key", 0);
```
3. 传递对象:
```java
// 创建Intent对象
Intent intent = new Intent(this, YourActivity.class);
// 添加数据
YourObject obj = new YourObject();
intent.putExtra("key", obj);
// 启动Activity
startActivity(intent);
```
在目标活动中获取数据:
```java
// 获取Intent对象
Intent intent = getIntent();
// 获取数据
YourObject obj = (YourObject) intent.getSerializableExtra("key");
```
以上是一些传递数据的示例,你可以根据需要选择相应的方法。在接收到数据后,你可以根据数据类型进行转换并进行相应的处理。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)