Intent是Android中各个组件之间进行交互的一种重要方式
他不仅可以表示当前组件想要执行的步骤,而且还可以在不同组件之间传递数据
大致分为两种:显性Intent、隐性Intent
<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"
tools:context="com.example.administrator.activitytest.FirstActivity">
<Button
android:id="@+id/first_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="button 1"/>
LinearLayout>
2.#####1.修改SecondActivity的布局文件:添加按钮,设置id
<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"
tools:context="com.example.administrator.activitytest.SecondActivity">
<Button
android:id="@+id/second_btn"
android:layout_width="match_parent"
android:layout_height="50sp"
android:text="button 2" />
LinearLayout>
3.修改FirstActivity活动:实现创建按钮对象,设置监听,实现跳转
public class FirstActivity extends AppCompatActivity {
//创建按钮对象 create button object
private Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//绑定id bind id
startBtn = findViewById(R.id.first_btn);
//设置监听 set monitor
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实现跳转
// achieve goto
//创建Intent对象 参数1:当前上下文环境,参数2:将要跳转的活动页面
// create Intent object (param1: current context param2:will goto context
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
//启动跳转 start goto
startActivity(intent);
}
});
}
}
隐式Intent的比较含蓄,他并不会明确指出将要启动的活动,而是指定一系列更为抽象的action和category等信息,然后由系统去分析intent的意图,最后找到合适的活动去启动。
1.修改Manifest中的SecondActivity的启动方式:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.activitytest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.administrator.activitytest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
intent-filter>
activity>
application>
manifest>
public class FirstActivity extends AppCompatActivity {
//创建按钮对象 create button object
private Button startBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
//绑定id bind id
startBtn = findViewById(R.id.first_btn);
//设置监听 set monitor
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//实现跳转
// achieve goto
//创建Intent对象 参数1:启动什么样的action
// create Intent object (param1: start what action)
Intent intent = new Intent("com.example.administrator.activitytest.ACTION_START");
startActivity(intent);
}
});
}
}
需要注意的是每个Intent只能指定一个action,但却可以指定多个category,我们来添加一个自定义的吧
<intent-filter>
<action android:name="com.example.administrator.activitytest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="com.example.administrator.activitytest.MY_CATEGORY"/>
intent-filter>
2.修改FirstActivity中的启动方式:
Intent intent = new Intent("com.example.administrator.activitytest.ACTION_START");
//添加自定义的category add user defined category
intent.addCategory("com.example.administrator.activitytest.MY_CATEGORY");
startActivity(intent);
//创建Intent对象 参数:启动什么样的action,ACTION_VIEW为系统内部的动作
// create Intent object (param1: start what action),ACTION_VIEW is system action
Intent intent = new Intent(Intent.ACTION_VIEW);
//给Intent设置数据,将网址通过parse解析然后来设置网址
//set Intent data ,put website parsed Uri object
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
<intent-filter>
<action android:name="com.example.administrator.activitytest.ACTION_START"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
intent-filter>