Android Intent使用
时间: 2023-11-10 12:49:16 浏览: 44
Android Intent是用于在应用程序之间传递消息的一种机制。您可以使用Intent在应用程序之间传递数据,启动活动,启动服务等。以下是一些常用的Intent用法:
1. 启动一个Activity:
```java
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
```
2. 传递数据到Activity:
```java
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data", "Hello World!");
startActivity(intent);
```
在接收Activity中,您可以使用以下代码获取数据:
```java
String data = getIntent().getStringExtra("data");
```
3. 启动一个Service:
```java
Intent intent = new Intent(this, MyService.class);
startService(intent);
```
4. 发送广播:
```java
Intent intent = new Intent();
intent.setAction("com.example.MY_ACTION");
sendBroadcast(intent);
```
5. 打开网页:
```java
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.csdn.net"));
startActivity(intent);
```
这些只是Intent的一些基本用法,您可以根据需要使用更多的Intent功能。