先看效果图

概述

在介绍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. ASP.NET Web API 创建帮助页

    1. 安装 Microsoft.AspNet.WebApi.HelpPage 程序包 Install-Package Microsoft.AspNet.WebApi.HelpPage 2. 注册 Ar ...

  2. IDDD 实现领域驱动设计-架构之经典分层

    上一篇:<IDDD 实现领域驱动设计-上下文映射图及其相关概念> 在<实现领域驱动设计>书中,分层的概念作者讲述的很少,也就几页的内容,但对于我来说,有很多的感触需要诉说.之前 ...

  3. 【记录】Install-Package : “Unity”已拥有为“CommonServiceLocator”定义的依赖项。

    在使用 NuGet 安装 Unity 的时候,安装命令:install-package unity. 但是会莫名奇妙的报如下错误: “Unity”已拥有为“CommonServiceLocator”定 ...

  4. publishing failed with multiple errors resource is out of sync with the file system--转

    原文地址:http://blog.csdn.net/feng1603/article/details/7398266 今天用eclipse部署项目遇到"publishing failed w ...

  5. Callbacks vs Events

    前言:本文翻译自Dean Edwards的一篇文章,原文地址:http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/. 文章主要指出了 ...

  6. CSS实现单行与多行文字省略(truncation)

    在上一篇文章小div布局之卡片堆叠(card-stacking)中有多行文字溢出省略的效果,这篇文章就对这种效果(包括单行文字溢出省略)的实现做个简单的记录,以防自己忘记.具体来说,就是要实现这种文字 ...

  7. ime-mode:disabled 关闭文本框输入法

    在用户输入数字的表单中,需要禁止用户输入中文.符号等,减少用户输入出错误的可能性,CSS可以实现此功能. ime-mode的语法解释如下: ime-mode : auto | active | ina ...

  8. AngularJs $q promise

    angularjs提供的$q服务是对Promises规范的一个实现.$q服务可以把一段异步的代码封装成同步的样式. 为啥是样式,因为异步还是异步,它并不会柱塞代码,只是看起来像同步代码. $q.whe ...

  9. MindMup 是一个开源的、在线的、简单的思维导图工具

    MindMup是一个开源.在线的思维导图工具:它有以下特点: 开源 在线 导图可存放在网站(公有,要是在不同的终端浏览的话需要记住导图的网址)或google driver(私有),无用户名密码 很方便 ...

  10. JS产生随机数的几个用法!

    <script> function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); ret ...