Intent的分类
Intent分为显式Intent和隐式Intent。显式Intent常用在本应用之间的组件跳转,隐式Intent常用于在不同应用之间组件的跳转。通过Intent,可以向Android提交一个请求,Android会根据Intent的意愿来选择合适的组件来响应请求。
隐式调用看这里:Android入门练习——Intent的隐式调用
显式Intent
一般通过指定Intent组件名称来实现。显示Intent明确指定了要激活的组件是哪个组件,一般是在应用程序内部使用。
Intent的ComponentName
属性:显示方式直接通过组件名称ComponentName来查找,组件名称包含包名称和类名称,被声明在AndroidManifest.xml文件中。
常见方法:
setClass( Context packageContext, Class ClassNmae.class)
, 此方法不能实现跨应用调用setClass( String packageName, String classname )
, 此方法能够实现跨应用调用setComponent(ComponentName component)
,此方法能够实现跨应用调用
其实上面三种方法构造的本质是一样的,即创建一个ComponentName需要指定包名和类名,这样就可唯一确定一个组件类。
但是注意: 若是打开其他工程中的非主界面,则需要在其他工程中对应的AndroidManifest.xml文件中,在相应的activity中,添加属性android:exported=“true”,表示允许其他工程打开此Activity
实例:
效果:
显式intent四种方法实现效果是一样的,打开其他工程中的Activity打开的是之前intentDemo1的内容(详见:https://blog.csdn.net/qq_43145926/article/details/90036825)
- 主界面activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显式Intent的第一种方式" />
<Button
android:id="@+id/btn_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显式Intent的第二种方式" />
<Button
android:id=