Intent的属性及Intent-filter配置——Component属性

时间:2023-03-09 08:54:50
Intent的属性及Intent-filter配置——Component属性

Intent的Component属性需要接受一个ComponentName对象,ComponentName对象包含如下几个构造器。

  • ComponentName(String pkg,String cls):创建pkg所在包下的cls类所对应的组件。
  • ComponentName(Context pkg,String cls):创建pkg所对应的包下的cls类所对应的组件。
  • ComponentName(Context pkg,Class<?> cls):创建pkg所对应的包下的cls类所对应的组件。

上面构造器的本质就是一个,这说明创建一个ComponentName需要制定包名和类名——这就可唯一地确定一个组件类,这样应用程序即可根据给定的组件类去启动特定的组件。

除此之外,Intent还包含了如下三个方法。

  • setClass(Context packageContext,Class<?> cls):设置该Intent将要启动的组件对应的类。
  • setClassName(Context packageContext,String className):设置该Intent将要启动的组件对应的类名。
  • setClassName(String packagName,String className):设置该Intent将要启动的组件对应的类名。

指定了Component属性的Intent已经明确了它将要启动启动哪个组件,因此这种Intent也被称为显式Intent,没有指定Component属性的Intent被称为隐士Intent——隐士Intent没有明确要启动哪个组件,应用将会根据Intent指定的规划去启动符合条件的组件,但具体是哪个组件不确定。

下面的示例程序示范了如何通过隐士Intent(指定了Component属性)来启动另一个Activity。该程序的界面布局很简单,界面中只有一个按钮,用户单击该按钮将会启动另一个Activity。该程序的界面布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动第二个Activity"/>
</LinearLayout>

改程序的Java代码如下:

package com.example.studyintent;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class ComponentAttr extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_component_attr);
Button bn=(Button)findViewById(R.id.bn);
//为bn按钮绑定事件监听器
bn.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个ComponentName对象
ComponentName comp=new ComponentName(ComponentAttr.this,SecondActivity.class);
Intent intent=new Intent();
//为Intent设置Component属性
intent.setComponent(comp);
startActivity(intent); }});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.component_attr, menu);
return true;
} }

上面的程序中三行粗体字代码用于创建ComponentName对象,并将该对象设置成Intent对象的Component属性,这样应用程序即可根据该Intent的“意图”去启动指定组件。

实际上,上面三行粗体字代码完全可以简化为如下形式:

//根据指定组件类来创建Intent

Intent intent=new Intent(ComponentAttr.this,SecondActivity.class);

从上面的代码可以看出,当程序要为Intent设置Component属性时,实际上Intent已经提供了一个简化的构造器,这样方便程序直接指定其他组件。

当程序通过Intent的Component(明确指定了启动哪个组件)启动特定组件时,被启动组件几乎不需要使用<intent-filter.../>元素进行配置。

程序中的SecondActivity也很简单,它的界面布局中只包含一个简单的文本框,用于显示该Activity对应的Intent的Component属性的包名、类名,该Activity的Java代码如下。

package com.example.studyintent;

import java.util.Set;

import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.view.Menu;
import android.widget.EditText; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second); EditText show=(EditText)findViewById(R.id.show);
//获取该Activity对应的Intent的Component属性
ComponentName comp=getIntent().getComponent();
//显示该ComponentName对象的包名、类名
show.setText("组件包名为:"+comp.getPackageName()+"\n组件类名为:"+comp.getClassName());
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
} }

运行上面的程序,通过第一个Activity中的按钮进入第二个Activity将可以看到如图5.1所示的界面。

Intent的属性及Intent-filter配置——Component属性