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="@+id/btn_03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显式Intent的第三种方式" />
<Button
android:id="@+id/btn_04"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="打开其他工程中的Activity" />
<Button
android:id="@+id/btn_05"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显式Intent的第四种方式" />
</LinearLayout>
- activity02.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是Activity02"
/>
</LinearLayout>
- MainActivity.java
public class MainActivity extends Activity {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn_01);
btn2 = (Button) findViewById(R.id.btn_02);
btn3 = (Button) findViewById(R.id.btn_03);
btn4 = (Button) findViewById(R.id.btn_04);
btn5 = (Button) findViewById(R.id.btn_05);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClassName(MainActivity.this, "com.example.intentdemo2.SecondActivity");
startActivity(intent);
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClassName("com.example.intentdemo2", "com.example.intentdemo2.SecondActivity");
startActivity(intent);
}
});
btn4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setClassName("com.example.intentdemo1", "com.example.intentdemo1.MainActivity");
startActivity(intent);
}
});
btn5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setComponent(new ComponentName(MainActivity.this, SecondActivity.class));
startActivity(intent);
}
});
}
}
- SecondActivity.java
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
}
}
- AndroidManifst.xml
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>