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属性的更多相关文章

  1. SpringBoot 属性配置文件数据注入配置和yml与properties区别

    前言 我们知道SpringBoot 通过配置类来解放一堆的xml文件配置,通属性配置文件,来进行,系统全局属性配置,这样极大的简化了我们开发过程,java web 也可以甜甜的从此 快速配置 Spri ...

  2. 使用 Fluent API 配置/映射属性和类型(摘自微软Data Access and Storage)

    使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...

  3. 使用Fluent API 配置/映射属性和类型

    Code First约定-Fluent API配置 使用Fluent API 配置/映射属性和类型 简介 通常通过重写派生DbContext 上的OnModelCreating 方法来访问Code F ...

  4. 使用 Fluent API 配置/映射属性和类型

    使用 Fluent API 配置/映射属性和类型 使用实体框架 Code First 时,默认行为是使用一组 EF 中内嵌的约定将 POCO 类映射到表.但是,有时您无法或不想遵守这些约定,需要将实体 ...

  5. Intent属性详解一 component属性

    先看效果图 概述 在介绍Component之前,我们首先来了解ComponentName这个类:ComponentName与Intent同位于android.content包下,我们从Android官 ...

  6. 在Android中Intent的概念及应用(一)——显示Intent和隐式Intent

    Intent寻找目标组件的两种方式: 显式Intent:通过指定Intent组件名称来实现的,它一般用在知道目标组件名称的前提下,一般是在相同的应用程序内部实现的. 隐式Intent:通过Intent ...

  7. Android开发学习笔记:浅谈显示Intent和隐式Intent

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://liangruijun.blog.51cto.com/3061169/655132 ...

  8. Intent官方教程(2)Intent的两种类型

    Intent Types There are two types of intents: Explicit intents specify the component to start by name ...

  9. spring filter 配置

    web xml <filter>    <filter-name>DelegatingFilterProxy</filter-name>    <filter ...

随机推荐

  1. php url 伪静态

    手册上说:   'PATH_INFO'    包含由客户端提供的.跟在真实脚本名称之后并且在查询语句(query string)之前的路径信息,如果存在的话.例如,如果当前脚本是通过 URL http ...

  2. 基于心跳的socket长连接

    http://coach.iteye.com/blog/2024444 基于心跳的socket长连接 博客分类: http socket 案例: 心跳: socket模拟网页的报文连接某个网站,创建t ...

  3. Android网络开发之Volley--Volley基本用法JsonObjectReques(二)

    1.JsonObjectRequest用法 用法和StringRequest基本相同,主要分为3步: (1).实例化一个RequestQueue对象 (2).设置JsonObjectRequest对象 ...

  4. springMVC和spring的集成

    主要是2点 1.web.xml里 配置spring所需的listener 2. 新增applicationContext.xml, 配置注入的bean 3. 使用注解获取bean @Resource ...

  5. Gson通过借助TypeToken获取泛型参数的类型的方法

    最近在使用Google的Gson包进行Json和Java对象之间的转化,对于包含泛型的类的序列化和反序列化Gson也提供了很好的支持,感觉有点意思,就花时间研究了一下. 由于Java泛型的实现机制,使 ...

  6. 如何用PC标签在列表页中调出文章内容 phpcms

    如何用PC标签在列表页中调出文章内容 phpcms v9 moreinfo=”"参数说明 {pc:content action="lists" catid="$ ...

  7. ionic中将service中异步返回的数据赋值给controller的$scope

    1.service中异步获取数据实例 angular.module('starter.services', []) .factory('Chats', function($http,$q) {//定义 ...

  8. DNS原理及其解析过程

    网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所以计算机在网络上进行通讯时只能识别如“202.96.134.133” 之类的IP地址,而不能认识域名.我们无法记住10个以上IP地 ...

  9. Ubuntu和win10双系统Grup无法引导解决方案

    通常我们经常安装双系统, 但是有时候安装完系统无法正常引导, 以下就说明Ubuntu和win10双系统, win10在grub界面不断循环的解决方案 直接在win10启动项目上按e进入编辑模式 在文档 ...

  10. CentOS7 开源跳板机(堡垒机) Jumpserver

    开源跳板机(堡垒机)Jumpserver 环境 CentOS 7   x64       关闭 selinux  firewalld jumpserver: 172.24.0.14 testserve ...