先看效果图

概述

在介绍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. Android 之 ProgressDialog用法介绍

    布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...

  2. 清除页面广告?身为前端,自己做一款简易的chrome扩展吧

    大家肯定有这样的经历,浏览网页的时候,左右两端广告,诸如“屠龙宝刀,点击就送”,以及最近火的不行的林子聪37传奇霸业什么“霸业面具,霸业吊坠”的魔性广告总是充斥我们的眼球. 当然有现成的扩展程序或者插 ...

  3. MySQL学习笔记一:常用显示命令

    1.开启和关闭MySQL服务 WIN平台:NET START MYSQL55 :NET STOP MYSQL55 Linux平台:service mysql start : service mysql ...

  4. Windows Server 2008 R2 WEB服务器配置系列文章索引

    最近这段时间趁天翼云1元主机活动,购买了一个1元主机,主要是为了写一些服务器配置的教程. 已经完成如下几篇文章,送给大家. 国内云主机比较 天翼云/阿里云/腾讯云 Windows Server 200 ...

  5. react+redux教程(四)undo、devtools、router

    上节课,我们介绍了一些es6的新语法:react+redux教程(三)reduce().filter().map().some().every()....展开属性 今天我们通过解读redux-undo ...

  6. spring源码分析之定时任务Scheduled注解

    1. @Scheduled 可以将一个方法标识为可定时执行的.但必须指明cron(),fixedDelay(),或者fixedRate()属性. 注解的方法必须是无输入参数并返回空类型void的. @ ...

  7. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

  8. 遍历迭代map的集中方法

    public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...

  9. Ajax 与 XmlHttpRequest

    AJAX描述了确保Web应用在Web服务器请求新数据的情况下也能(几乎)实时反应的一种方法.具体地说,AJAX只是一些建立已久的技术的相互作用,从HTML.XHTML和HTTP,到JavaScript ...

  10. sed命令使用详解归纳

    用法 sed [option] 'Address Command' yourfile e.g. sed -n '2,13p' lineuser #打印文件lineuser中第2~13行的内容,-n为o ...