android ComponentName

从一个activity跳转到另一个activity有多种方法

ComponentName

 //此为一个按钮相应的代码摘录       ComponentName comp = new ComponentName(ComponentAttr.this,
                        SecondActivity.class); //设置跳转的activity
Intent intent = new Intent();
                // 为Intent设置Component属性
intent.setComponent(comp);
startActivity(intent);

跳转的页面能够获取到ComponentName的相关信息

ComponentName comp = getIntent().getComponent();
// 显示该ComponentName对象的包名、类名
show.setText("组件包名为:" + comp.getPackageName()
+ "\n组件类名为:" + comp.getClassName());

 

使用intent 的action 属性 和 <intent-filter?

public final static String CRAZYIT_ACTION ="org.crazyit.intent.action.CRAZYIT_ACTION"

//此为按钮响应 Intent intent = new Intent();
                // 为Intent设置Action属性(属性值就是一个普通字符串)
  intent.setAction(ActionAttr.CRAZYIT_ACTION);
  startActivity(intent);

跳转到的activity 在配置文件中增加了

<activity android:name=".SecondActivity"
android:label="@string/app_name">
  <intent-filter>
  <!-- 指定该Activity能响应Action为指定字符串的Intent -->
  <action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
  <!-- 指定该Activity能响应Action属性为helloWorld的Intent -->
  <action android:name="helloWorld" />
  <!-- 指定该Action能响应Category属性为指定字符串的Intent -->
  <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

 

intent action category

// 定义一个Action常量
final static String CRAZYIT_ACTION = "org.crazyit.intent.action.CRAZYIT_ACTION";
// 定义一个Category常量
final static String CRAZYIT_CATEGORY ="org.crazyit.intent.category.CRAZYIT_CATEGORY";

//跳转代码Intent intent = new Intent();
                // 设置Action属性
intent.setAction(ActionCateAttr.CRAZYIT_ACTION);
                // 添加Category属性
 intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY);
startActivity(intent);

//配置文件

<activity android:name=".SecondActivity"
android:label="@string/app_name">
<intent-filter>
<!-- 指定该Activity能响应action为指定字符串的Intent -->
<action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
<!-- 指定该Activity能响应category为指定字符串的Intent -->
<category android:name="org.crazyit.intent.category.CRAZYIT_CATEGORY" />
<!-- 指定该Activity能响应category为android.intent.category.DEFAULT的Intent -->
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

 

intent 调用系统通讯录

    Intent intent = new Intent();
     //设置Intent的Action属性
    intent.setAction(Intent.ACTION_GET_CONTENT);
      //设置Intent的Type属性
    intent.setType("vnd.android.cursor.item/phone");
     // 启动Activity,并希望获取该Activity的结果
     startActivityForResult(intent, PICK_CONTACT);

回调方法

public void onActivityResult(int requestCode
        , int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode)
        {
            case (PICK_CONTACT):
                if (resultCode == Activity.RESULT_OK)
                {
                    // 获取返回的数据
                    Uri contactData = data.getData();
                    CursorLoader cursorLoader = new CursorLoader(this
                        , contactData, null, null, null, null);
                    // 查询联系人信息
                    Cursor cursor = cursorLoader.loadInBackground();
                    // 如果查询到指定的联系人
                    if (cursor.moveToFirst())
                    {
                        String contactId = cursor.getString(cursor
                            .getColumnIndex(ContactsContract.Contacts._ID));
                        // 获取联系人的名字
                        String name = cursor.getString(cursor
                            .getColumnIndexOrThrow(
                            ContactsContract.Contacts.DISPLAY_NAME));
                        String phoneNumber = "此联系人暂未输入电话号码";
                        //根据联系人查询该联系人的详细信息
                        Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + contactId, null, null);
                        if (phones.moveToFirst())
                        {
                            //取出电话号码
                            phoneNumber = phones
                                .getString(phones
                                .getColumnIndex(ContactsContract
                                .CommonDataKinds.Phone.NUMBER));
                        }
                        // 关闭游标
                        phones.close();
                        EditText show = (EditText) findViewById(R.id.show);
                        //显示联系人的名称
                        show.setText(name);
                        EditText phone = (EditText) findViewById(R.id.phone);
                        //显示联系人的电话号码
                        phone.setText(phoneNumber);
                    }
                    // 关闭游标
                    cursor.close();
                }
                break;
        }
    }
时间: 2024-08-30 05:55:39

android ComponentName的相关文章

深入分析:Android中app之间的交互(二,使用ComponentName)

在前一篇相关主题的博文中我们了解了如何使用Action来启动当前应用之外的Activity处理我们的业务逻辑,在本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互. 在介绍Component之前,我们首先来了解ComponentName这个类:ComponentName与Intent同位于android.content包下,我们从Android官方文档中可以看到,这个类主要用来定义可见一个应用程序组件,例如:Activity,Service,Broadcast

Android使用ComponentName组件简单示例

Intent中可以直接使用Intent.setClass(),也可以使用组件Component. 简单的使用方式如下: 1 package com.example.test; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.ComponentName; 6 import android.content.Intent; 7 import android.view.Menu;

Android 四大组件 Service 服务

1.Service简介 按照使用范围分类: 类别 优点 缺点 区别 应用 本地服务 Local  Service 本地服务在一定程度上节约了资源,另外本地服务因为是在同一进程,因此不需要IPC,也不需要AIDL.相应bindService会方便很多. 主进程被Kill后,服务便会终止. 本地服务依附在主进程上,而不是独立的进程,用于应用程序内部 . 音乐播放服务 远程服务 Remote Service 对应进程名格式为所在包名加上指定的android:process字符串.由于是独立的进程,因此

【翻译】Android Interface Definition Language (AIDL)

参考地址:https://developer.android.com/guide/components/aidl.html Android Interface Definition Language (AIDL) AIDL (Android Interface Definition Language) is similar to other IDLs you might have worked with. It allows you to define the programming inter

android Application Component研究之Activity(一)

http://blog.csdn.net/windskier/article/details/7096521 终于下定决心写写ActivityManagerService的源码分析的文章了,ActivityManagerService 业务的整个逻辑关系被各种复杂的数据结构包裹着,因此对ActivityManagerService 的分析主要就是对各种数据结构的分析,明白了这些数据结构,理解ActivityManagerService的业务内容就水到渠成了. AMS提供了一个ArrayList

Android加壳原理分析

0x00 阅读本文前,建议读者首先阅读Android加壳原理,参考文章Android中的Apk的加固(加壳)原理解析和实现.如果没有看过这篇文章,本文理解起来比较困难. 0x01 下面我们来分析脱壳代码为什么要这样写,核心脱壳代码在ProxyApplication类里面,首先执行成员方法attachBaseContext,然后执行成员方法onCreate. 那么attachBaseContext是什么时候被执行的呢,为什么先于onCreate执行呢?那就需要看Android的源码了,我们选用的是

Android之Intent

前言:大家都知道Android程序的实现一般都由四大组件构成: Activity :Android程序实现功能的主体,提供了和客户交互的界面,也提供了和后台交互的功能. Service :是一个没有界面的activity,主要用于后台运行的程序. Broadcast :是当前程序和系统之间通信的工具. ContentProvider :android程序,管理资源的一种工具. 上面这4个组件都需要在manifest里面注册才能够使用,manifest就相当于android程序的大管家.当然光有组

Android Service完全解析,关于服务你所需知道的一切(下) (转载)

转自:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_blog/article/details/9797169 在 上一篇文章中,我们学习了Android Service相关的许多重要内容,包括Service的基本用法.Service和Activity进行通信.Service的销毁方式. Service与Thread的关系.以及如何创建前台Service.以上

Pro Android学习笔记(十一):了解Intent(中)

Intent的构成 Intent可以带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个intent至少携带上述的一个内容. Action.Action名,在上一笔记中已经给出两种例子,一种是系统自带的,如Intent.ACTION_DAIL,一种是开发者通过AndroidManifest.xml进行注册的,在创建intent时给出:Intent intent=new Intent(Strin