Intent属性详解一 component属性

先看效果图

概述

在介绍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下载
最后,以上例子都来源与安卓无忧,请去应用宝或者豌豆荚下载:例子源码,源码例子文档一网打尽

时间: 2024-08-02 10:44:29

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

WPF依赖属性详解

WPF依赖属性详解 WPF 依赖属性 英文译为 Dependency Properties,是WPF引入的一种新类型的属性,在WPF中有着极为广泛的应用,在WPF中对于WPF Dependency Properties 的使用贯穿样式的使用,数据绑定,动画等等,在刚刚接触Dependency Properties的时候可能觉得有些奇怪,但是,当你了解他要解决的问题的时候,你可能就不觉得奇怪了.Dependency Properties第一个要解决的问题就是控件的属性共享问题,由于大部分的WPF控

EditText属性详解

关于EditText属性详解很详细的记录,转过来收着 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 (2)在代码里设置: 通过设置EditText的setTransformationMethod()方法来实现隐藏密码或这显示密码. editText.setTransformationMethod(PasswordTransformationMethod.g

css动画-animation各个属性详解(转)

CSS3的animation很容易就能实现各种酷炫的动画,虽然看到别人的成果图会觉得很难,但是如果掌握好各种动画属性,做好酷炫吊炸天的动画都不在话下,好,切入正题. 一.动画属性: 动画属性包括:①animation-name,②animation-duration,③animation-timing-function, 以下是各属性详解: 1.animation-name:指定要绑定到选择器的关键帧的名称. 2.animation-duration:定义动画完成一个周期需要多少秒或毫秒 3.a

jQuery Event.which 属性详解

jQuery Event.which 属性详解 which属性用于返回触发当前事件时按下的键盘按键或鼠标按钮. 对于键盘和鼠标事件,该属性用于确定你按下的是哪一个键盘按键或鼠标按钮. which属性对DOM原生的event.keyCode和event.charCode进行了标准化. 适用的事件类型主要有键盘事件:keypress.keydown.keyup,以及鼠标事件:mouseup.mousedown. 该属性属于jQuery的Event对象(实例). 语法 jQuery 1.1.3 新增该

Hibernate fetch属性详解

主要参考 :http://4045060.blog.51cto.com/4035060/1088025 一.hibernate抓取策略(单端代理的批量抓取fetch=select(默认)/join) 1)保持默认,同fetch="select" <many-to-one name="group" column="group_id" fetch="select" /> 另外发送一条select语句抓取当前对象关联实体

MWPhotoBrowser 属性详解 和代理解释

--------0.MWPhoto简单属性解释---------------- MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:@"http://farm4.static.flickr.com/3629/3339128908_7aecabc34b.jpg"]]; photo.caption = @"在将photo添加到数组中时,可以在这里设置标题名字"; photo = [MWPhotophot

详解android:scaleType属性

详解android:scaleType属性 转自:http://blog.csdn.net/encienqi/article/details/7913262    http://juliaailse.iteye.com/blog/1409317 Android:scaleType是控制图片如何resized/moved来匹对ImageView的size. ImageView.ScaleType / android:scaleType值的意义区别: CENTER /center  按图片的原来si

Android textAppearance的属性设置及TextView属性详解

textAppearance的属性设置 android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceMedium" android:textAppearance="?android:attr/textAppearanceLarge" TextView属性详解 android:autoLi

Shape 各种属性详解

本文来自:http://blog.csdn.net/brokge/article/details/9713041 简介: 作用:XML中定义的几何形状 位置:res/drawable/文件的名称.xml 使用的方法: Java代码中:R.drawable.文件的名称 XML中:Android:background="@drawable/文件的名称" 属性: <shape>形状 Android:shape=["rectangle" | "oval