一、Intent的介绍
(一)Intent是什么
Intent是一种运行时绑定机制,用于程序运行的过程中连接两个不同的组件。简而言之,Intent是一种协助应用间的交互与通讯的机制。
(二)Intent相关属性
属性 | 描述 |
---|---|
component(组件) | 目的组件 |
action(动作) | 表现意图的动作 |
category(类别) | 表现动作的类别 |
data(数据) | 与动作要操作的数据 |
type(数据类型) | data的描述 |
extras(扩展信息) | 扩展信息 |
二、Intent的实际使用
(一)显示意图
明确指明组件名称。
1.方法一
//this指向当前的Activity,后面填写内容为需要跳转的Activity
intent.setClass(this,ClockActivity.class);
2.方法二
///this指向当前的Activity,后面是要跳转Activity的全类名
intent.setClassName(this,"com.example.intentthreetype.DateActivity");
3.方法三
//前面内容是要打开Activity的项目名,后面是要跳转项目中Activity的全类名
intent.setClassName("com.example.helloworld","com.example.helloworld.MainActivity");
(二)隐式意图
不直接指明组件名称。
intent-filter(意图过滤器)
通过设置AndroidManifest.xml文件,对activity进行添加意图过滤器,设置action。
<activity android:name=".Show_Image">
<intent-filter>
<action android:name="openimage" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
三、实例
(一)显示意图实例
通过三种方式来实现进入另外一个Activity
MainActivity代码(只给出比较重要部分的代码)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button type1_btn;
private Button type2_btn;
private Button type3_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
type1_btn=findViewById(R.id.type1_btn);
type2_btn=findViewById(R.id.type2_btn);
type3_btn=findViewById(R.id.type3_btn);
type1_btn.setOnClickListener(this);
type2_btn.setOnClickListener(this);
type3_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent=new Intent();
switch (view.getId())
{
case R.id.type1_btn:
intent.setClass(this,ClockActivity.class);
break;
case R.id.type2_btn:
intent.setClassName(this,"com.example.intentthreetype.DateActivity");
break;
case R.id.type3_btn:
intent.setClassName("com.example.helloworld","com.example.helloworld.MainActivity");
break;
}
startActivity(intent);
}
}
最终显示效果
![]() |
![]() |
![]() |
![]() |
(二)隐式意图实例
通过按钮进入另外一个Activity
MainActivity代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_image;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_image=findViewById(R.id.imageshow);
btn_image.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction("openimage");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}
AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imageshow">
<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/Theme.ImageShow">
<activity android:name=".Show_Image">
<intent-filter>
<action android:name="openimage" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
显示效果
![]() |
![]() |