1. 编程实现Intent的显示跳转。在MainActivity中定义Intent,启动SeconActivity,能够获取SecondActivity的返回值; 2、使用Intent过滤器实现隐式跳转。在MainActiviy中定义Intent,要求必须用到自定义的action、category、data来启动ThirdActivity; 3. 实现通过Intent调用android内置浏览器; 4. 通过Intent实现广播消息的接收。在MainActivity中定义Intent,将界面上某文本框中的内容以广播消息的形式发出。广播消息可以使用Toast或Logcat。
时间: 2024-04-25 20:25:51 浏览: 98
1. 显示跳转并获取返回值:
在 MainActivity 中定义 Intent,并使用 startActivityForResult() 方法启动 SecondActivity。在 SecondActivity 中设置返回结果,并使用 setResult() 方法返回结果。在 MainActivity 中重写 onActivityResult() 方法获取返回结果。
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1;
private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button goToSecondButton = findViewById(R.id.go_to_second_button);
resultTextView = findViewById(R.id.result_text_view);
goToSecondButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
String result = data.getStringExtra(SecondActivity.EXTRA_RESULT);
resultTextView.setText(result);
}
}
}
```
SecondActivity.java:
```java
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_RESULT = "com.example.myapplication.RESULT";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button returnButton = findViewById(R.id.return_button);
returnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String result = "Hello, MainActivity!";
Intent intent = new Intent();
intent.putExtra(EXTRA_RESULT, result);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
2. 使用 Intent 过滤器实现隐式跳转:
在 MainActivity 中定义 Intent,并设置自定义的 action、category、data。在 ThirdActivity 的 AndroidManifest.xml 文件中使用 intent-filter 元素来指定响应的 Intent。
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button goToThirdButton = findViewById(R.id.go_to_third_button);
goToThirdButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("com.example.myapplication.ACTION_THIRD");
intent.addCategory("com.example.myapplication.CATEGORY_THIRD");
intent.setData(Uri.parse("https://www.example.com"));
startActivity(intent);
}
});
}
}
```
ThirdActivity 的 AndroidManifest.xml:
```xml
<activity android:name=".ThirdActivity">
<intent-filter>
<action android:name="com.example.myapplication.ACTION_THIRD" />
<category android:name="com.example.myapplication.CATEGORY_THIRD" />
<data android:scheme="https" android:host="www.example.com" />
</intent-filter>
</activity>
```
ThirdActivity.java:
```java
public class ThirdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
}
}
```
3. 通过 Intent 调用 Android 内置浏览器:
在 MainActivity 中定义 Intent,并使用 setData() 方法设置要打开的网址。然后使用 startActivity() 方法启动隐式 Intent,Android 系统会自动选择合适的应用程序打开该网址。
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openBrowserButton = findViewById(R.id.open_browser_button);
openBrowserButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "https://www.example.com";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
}
}
```
4. 通过 Intent 实现广播消息的接收:
在 MainActivity 中定义 Intent,并使用 sendBroadcast() 方法将广播消息发送出去。然后在 BroadcastReceiver 的 onReceive() 方法中接收广播消息,并使用 Toast 或 Logcat 打印出来。
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
public static final String ACTION_MY_BROADCAST = "com.example.myapplication.ACTION_MY_BROADCAST";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendBroadcastButton = findViewById(R.id.send_broadcast_button);
sendBroadcastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = findViewById(R.id.edit_text);
String message = editText.getText().toString();
Intent intent = new Intent(ACTION_MY_BROADCAST);
intent.putExtra("message", message);
sendBroadcast(intent);
}
});
}
}
```
MyBroadcastReceiver.java:
```java
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
Log.d("MyBroadcastReceiver", message);
}
}
```
AndroidManifest.xml:
```xml
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.myapplication.ACTION_MY_BROADCAST" />
</intent-filter>
</receiver>
```
阅读全文