在Android系统中,应用程序一般都有多个Activity,Intent可以实现不同Activity之间的切换和数据传递,Intent启动Activity方式:显式启动和隐式启动。
显式启动:
按名称(完全限定类名)指定要启动的组件。 通常,会在自己的应用中使用显式Intent来启动组件,这是因为知道要启动的Activity或服务的类名。
创建显式Intent启动Activity或服务时,系统将立即启动Intent对象中指定的应用组件。
隐式Intent :
不指定特定的组件,而是声明要执行的常规操作,从而允许其他应用中的组件来处理它。
- Android系统根据Intent的动作和数据来决定启动哪一个Activity
- 在隐式启动时,Intent中只包含需要执行的动作和所包含的数据,而无需指明具体启动哪一个Activity,选择权由Android系统和最终用户来决定
1.运行图
界面:
显式启动:
隐式启动:
打开子活动:
确认按钮:
取消按钮:
2.代码
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp" />
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:onClick="explit"
android:text="显式启动"
android:textSize="28sp" />
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:onClick="implit"
android:text="隐式启动"
android:textSize="28sp" />
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:onClick="implit1"
android:text="打开子活动方式1"
android:textSize="28sp" />
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:onClick="implit2"
android:text="打开子活动方式2"
android:textSize="28sp" />
</LinearLayout>
(2)MainActivtiy.java
package com.example.activity;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText myinfo = null;
private ActivityResultLauncher<Intent> activityResultLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myinfo = findViewById(R.id.info);
activityResultLauncher = registerForActivityResult
(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getData() != null) {//没有请求码,单一处理
if (result.getResultCode() == RESULT_OK) {
String temp = result.getData().getStringExtra("reva");
myinfo.setText(temp);
} else {
myinfo.setText("对方没有返回值");
}
}
}
});
}
public void explit(View view) {
// Intent intent=new Intent(MainActivity.this,SecondActivity.class);
// intent.putExtra("value",myinfo.getText().toString());
// startActivity(intent);
SecondActivity.activityStart(MainActivity.this, myinfo.getText().toString());
}
public void implit(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(myinfo.getText().toString()));
startActivity(intent);
}
public void implit1(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("value", myinfo.getText().toString());
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String temp = data.getStringExtra("reva");
myinfo.setText(temp);
} else {
myinfo.setText("对方没有返回值");
}
break;
}
}
public void implit2(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("value", myinfo.getText().toString());
activityResultLauncher.launch(intent);
}
}
(3)activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接受的值:"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/receiveinfo"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="要返回的值:"
android:textSize="30sp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/returninfo"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:onClick="ok"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:onClick="cancel"
/>
</LinearLayout>
</LinearLayout>
(4)SecondActivity.java
package com.example.activity;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private TextView myview=null;
private EditText myedit=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
myview=findViewById(R.id.receiveinfo);
myedit=findViewById(R.id.returninfo);
Intent intent=getIntent();
String temp=intent.getStringExtra("value");
myview.setText(temp);
}
public static void activityStart(Context context,String data){
Intent intent=new Intent(context,SecondActivity.class);
intent.putExtra("value",data);
context.startActivity(intent);
}
public void ok(View view) {
String temp=myedit.getText().toString();
Intent intent=new Intent();
intent.putExtra("reva",temp);
setResult(RESULT_OK,intent);//设置返回值
finish();
}
public void cancel(View view) {
Intent intent=new Intent();
setResult(RESULT_CANCELED,intent);
finish();
}
}
增加一个新的活动SecondAcitivity,也能打开网页,则在AndroidManifest.xml如下图设置: