先看效果图

概述

在介绍Component之前,我们首先来了解ComponentName这个类;ComponentName与Intent同位于android.content包下,我们从Android官方文档中可以看到,这个类主要用来定义可见一个应用程序组件,例如:Activity,Service,BroadcastReceiver或者ContentProvider。

那么,如何用ComponentName来定义一个组件呢。这是ComponentName的构造函数:ComponentName(String pkg,String cls) 。我们知道在Android应用程序中如果要详细描述一个组件我们需要知道该组件所在的应用包名,也就是在AndroidManifest.xml文件中manifest根结点下的package=“XXX.XXXXX.XXXXX",还有组件在应用程序中的完整路径名,拿Activity来说,也就是activity节点中name属性的值。因此到这里我们也就明白了可以使用ComponentName来封装一个组件的应用包名和组件的名字。

我们已经知道,在Android中组件之间的交流往往使用意图(Intent)来完成的,那么在Intent中有一个方法可以封装一个ComponentName,最后我们在使用意图去完成我们需要实现的功能。

简单示例

下面我们用具体的代码来描述如何使用ComponentName来帮助我们与其他应用程序交互:

首先我们要新建两个Android应用程序,appsend和appreceiver。appreceiver的AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>  

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  

 package="com.example.appreceiver"

    android:versionCode="1"  

    android:versionName="1.0" >  

    <uses-sdk  

        android:minSdkVersion="11"  

        android:targetSdkVersion="18" />  

    <application  

        android:allowBackup="true"  

        android:icon="@drawable/ic_launcher"  

        android:label="@string/app_name"  

        android:theme="@style/AppTheme" >  

        <activity  

         android:name="com.example.appreceiver.MainActivity"</span></strong>  

            android:label="@string/app_name" >  

            <intent-filter>  

                <action android:name="android.intent.action.MAIN" />  

                <category android:name="android.intent.category.LAUNCHER" />  

            </intent-filter>  

        </activity>  

    </application>  

</manifest>  

appsend中的启动Activity的片段:

public void button(View view) {  

ComponentName cn=new ComponentName("com.example.appreceiver", "com.example.appreceiver.MainActivity");

    Intent intent = new Intent();  

   intent.setComponent(cn);</span></strong>  

    startActivityForResult(intent, 2);  

}  

.componentName(组件名称),指定Intent的目标组件的类名称。组件名称是可选的,如果填写,Intent对象会发送给指定组件名称的组件,否则也可以通过其他Intent信息定位到适合的组件。组件名称是个ComponentName类型的对象。

用法:

Intent intent = new Intent();

// 构造的参数为当前Context和目标组件的类路径名

ComponentName cn = new ComponentName(HelloActivity.this, "com.byread.activity.OtherActivity");

intent.setComponent(cn);

startActivity(intent);

相当于以下常用方法:

Intent intent = new Intent();

intent.setClass(HelloActivity.this, OtherActivity.class);

startActivity(intent);

Intent类中也包含一个初始化ComponentName的构造函数:

public Intent(Context packageContext, Class<?> cls) {

mComponent = new ComponentName(packageContext, cls);

}

Demo代码

package mm.shandong.com.testcomponent;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; public class TestComponentActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_component);
ComponentName componentName=getIntent().getComponent();
Toast.makeText(this,"组件包名:"+componentName.getPackageName()+
"。\n组件类名:"+componentName.getClassName(),Toast.LENGTH_LONG).show();
}
public void startActivityFirst(View view){
Intent intent=new Intent();
ComponentName componentName=new ComponentName(TestComponentActivity.this,TestComponentOtherActivity.class);
intent.setComponent(componentName);
startActivity(intent);
}
public void startActivitySecond(View view){
Intent intent=new Intent();
intent.setClass(TestComponentActivity.this,TestComponentOtherActivity.class);
startActivity(intent);
}
public void startActivityThird(View view){
Intent intent=new Intent(TestComponentActivity.this,TestComponentOtherActivity.class); startActivity(intent);
}
public void startActivityFourth(View view){
Intent intent=new Intent();
String packageName=getPackageName();
ComponentName componentName=new ComponentName(packageName,"mm.shandong.com.testcomponent.TestComponentOtherActivity");
intent.setComponent(componentName);
startActivity(intent);
}
public void startActivityFifth(View view){
Intent intent=new Intent();
ComponentName componentName=new ComponentName(TestComponentActivity.this,"mm.shandong.com.testcomponent.TestComponentOtherActivity");
intent.setComponent(componentName);
startActivity(intent);
}
}

本人微博:honey_11

Demo下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:例子源码,源码例子文档一网打尽

Intent属性详解一 component属性的更多相关文章

  1. unittest----常用属性详解(框架属性详解)

    很久没有写关于测试的随笔了,最近有空学习.整理一下关于unittest框架的知识. unittest单元测试框架,不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行. unittest ...

  2. WPF依赖属性详解

    WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency P ...

  3. Android组件---四大布局的属性详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4372222.html Android常见布局有下面几种: LinearL ...

  4. fiddler请求报文的headers属性详解

    fiddler请求报文的headers属性详解 headers的属性包含以下几部分. (1)Cache头域 在Cache头域中,通常会出现以下属性. 1. Cache-Control 用来指定Resp ...

  5. Flash播放控件属性详解

    Flash 播放控件属性详解 一.属性篇 1.AlignMode(读写)  语法:AlignMode As Long  说明:对齐方式(与SAlign 属性联动).当控件的长宽比例与影片不一致且WMo ...

  6. z-index属性详解

    z-index属性详解 目录 z-index属性详解 一.定义和用法 二.代码 三.效果图 一.定义和用法 z-index属性指定一个元素的堆叠顺序,也就是z轴 position属性定义的是x轴和y轴 ...

  7. Android零基础入门第80节:Intent 属性详解(下)

    上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...

  8. android:exported 属性详解

    属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...

  9. EditText属性详解

    关于EditText属性详解很详细的记录,转过来收着 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password=" ...

随机推荐

  1. 准备 KVM 实验环境 - 每天5分钟玩转 OpenStack(3)

    KVM 是 OpenStack 使用最广泛的 Hypervisor,本节介绍如何搭建 KVM 实验环境 安装 KVM 上一节说了,KVM 是 2 型虚拟化,是运行在操作系统之上的,所以我们先要装一个 ...

  2. ASP.NET MVC之如何看待内置配置来提高性能优化(四)

    前言 前几篇我们比较基础的讲了下MVC中的知识,这一节我们穿插点知识,讲讲MVC中我们可以提高性能的办法. Razor视图引擎优化(优化一) 我们知道默认情况下配置MVC去解析一个视图会首先约定通过查 ...

  3. 辨析relative与absolute

    谈起它们,想必大家都不陌生.relative,相对定位嘛:absolute,绝对定位嘛.但是它们到底是个啥东东呢? 看看w3c的定义,见下表 定位 含义 relative 元素框偏移某个距离.元素仍保 ...

  4. Rust初步(四):在rust中处理时间

    这个看起来是一个很小的问题,我们如果是在.NET里面的话,很简单地可以直接使用System.DateTime.Now获取到当前时间,还可以进行各种不同的计算或者输出.但是这样一个问题,在rust里面, ...

  5. PowerDesigner导出Report通用报表

    PowerDesigner导出Report通用报表 通用模板下载地址:http://pan.baidu.com/s/1c0NDphm

  6. php变量-单引号不编译,双引号编译

    <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); $sTemp = ...

  7. java 线程 Thread 使用介绍,包含wait(),notifyAll() 等函数使用介绍

    (原创,转载请说明出处!谢谢--http://www.cnblogs.com/linguanh/) 此文目的为了帮助大家较全面.通俗地了解线程 Thread 相关基础知识! 目录: --线程的创建: ...

  8. SDN/NFV若干问题

    1.首先谈一谈网络技术和组网技术的关系 网络可分为两层:业务网.承载网.业务网主要是组织业务系统,而承载网主要是用来传输信息流:包括传送网(点到点数据专线).数据网(端到端连接).内容分发网(点到多点 ...

  9. APP接口自动化测试JAVA+TestNG(一)之框架环境搭建

    前言 好久不曾写点啥,去年换到新公司组测试团队与培养建设花费大量时间与精力,终于架构成型与稳定有时间可以打打酱油了.很久没有总结点啥,提笔想写的内容太多,先放APP接口自动化的内容吧,这个估计大家比较 ...

  10. SqlServer基础之(触发器)

    概念:   触发器(trigger)是SQL server 提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动,而是由事件来触 ...