深入分析:Android中app之间的交互(二,使用ComponentName)
来源:互联网 发布:js 混淆加密 编辑:程序博客网 时间:2024/06/05 09:46
http://blog.csdn.net/huangyabin001/article/details/36626341
在前一篇相关主题的博文中我们了解了如何使用Action来启动当前应用之外的Activity处理我们的业务逻辑,在本篇笔记中我在简单介绍一下使用ComponentName来与当前应用之外的应用进行交互。
在介绍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"
- <span style="color:#cc0000;"> <strong>package="com.example.appreceiver"</strong></span>
- 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
- <strong><span style="color:#ff0000;"> 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) {
- <strong><span style="color:#ff0000;">ComponentName cn=new ComponentName("com.example.appreceiver", "com.example.appreceiver.MainActivity");</span></strong>
- Intent intent = new Intent();
- <strong><span style="color:#ff0000;">intent.setComponent(cn);</span></strong>
- startActivityForResult(intent, 2);
- }
完整案例,已经打包上传至csdn了,如有需要可以下载来看,点击打开链接
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- Android中app之间的交互(二,使用ComponentName)
- 深入分析:Android中app之间的交互(二)
- android ComponentName的使用 之app交互
- 深入分析:Android中app之间的交互(一)
- 深入分析:Android中app之间的交互(一,使用Action)
- 深入分析:Android中app之间的交互(一,使用Action)
- Android中app之间的交互(使用Action)
- Android ComponentName的使用
- Android 中使用 ComponentName 类
- Android APP之间的交互
- Android App之间是如何交互的?
- android studio 两个app之间的交互
- iOS 输入纯数字,有删除键哦!
- 如何防止session超时
- Java异常处理机制
- Chromium M34上Audio API(HTMLMediaElement)播放音乐不能获取currentTime属性更新
- MFC类CFileFind
- 深入分析:Android中app之间的交互(二,使用ComponentName)
- iOS 学习资料整理
- EasyUI——DataGrid的onClickRow事件
- Servlet 工作原理解析
- VC编译开关选项分析(debug和release版本的对比)
- flash上加链接j
- ListView实现上拉加载更多
- HTML 简介及简单的html标签
- Labview+USB+FPGA数据采集系统之总结