component namename的cls是全名还是短名

android笔记--Intent和IntentFilter详解 - 刘圣杰 - 博客园
Intent用于启动Activity, Service, 以及BroadcastReceiver三种组件, 同时还是组件之间通信的重要媒介.使用Intent启动组件的优势1, Intent为组件的启动提供了一致的编程模型. 无论想要启动的组件是Activity, Service, 还是BroadcastReceiver, 都可以使用Intent封装启动的意图.2, 在某些时候, 应用程序只是想启动具有某种特征的组件, 并不想和某个特定的组件耦合. 使用Intent可以方便的达到这种高层次解耦的目的.Intent的Component属性Intent对象的setComponent(ComponentName comp)方法用于设置Intent的Component属性. ComponentName包含如下几个构造器:ComponentName(String pkg, String cls)ComponentName(Context pkg, String cls)ComponentName(Context pkg, Class&?& cls)由以上的构造器可知, 创建一个ComponentName对象需要指定包名和类名--这就可以唯一确定一个组件类, 这样应用程序即可根据给定的组件类去启动特定的组件. 例如:ComponentName comp = new ComponentName(FirstActivity.this, SecondActivity.class);Intent intent = new Intent();intent.setComponent(comp);以上三句代码创建了一个intent对象, 并为其指定了Component属性, 完全等价于下面的代码:Intent intent = new Intent(FirstActivity.this, SecondActivity.class);除了使用setComponent() 之外, 还可以使用setClass(), setClassName()来显式指定目标组件, 还可以调用getComponent()方法获得Intent中封装的ComponentName对象.当程序采用这种形式启动组件时, 在Intent中明确的指定了待启动的组件类, 此时的Intent属于显式intent, 显式Intent应用场合比较狭窄, 多用于启动本应用中的component, 因为这种方式需要提前获知目标组件类的全限定名. 而隐式Intent则通过Intent中的action, category, data属性指定目标组件需要满足的若干条件, 系统筛选出满足所有条件的component, 从中选择最合适的component或者由用户选择一个component作为目标组件启动.如果Intent中指定了ComponentName属性, 则Intent的其他属性将被忽略.Intent的Action属性action属性是一个字符串, 代表某一种特定的动作. Intent类预定义了一些action常量, 开发者也可以自定义action. 一般来说, 自定义的action应该以application的包名作为前缀, 然后附加特定的大写字符串, 例如"cn.xing.upload.action.UPLOAD_COMPLETE"就是一个命名良好的action.Intent类的setAction()方法用于设定action, getAction()方法可以获取Intent中封装的action.以下是Intent类中预定义的部分action:ACTION_CALL--目标组件为activity, 代表拨号动作;ACTION_EDIT--目标组件为activity, 代表向用户显示数据以供其编辑的动作;ACTION_MAIN--目标组件为activity, 表示作为task中的初始activity启动;ACTION_BATTERY_LOW--目标组件为broadcastReceiver, 提醒手机电量过低;ACTION_SCREEN_ON--目标组件为broadcast, 表示开启屏幕.Intent的Category属性category属性也是一个字符串, 用于指定一些目标组件需要满足的额外条件. Intent对象中可以包含任意多个category属性. Intent类也预定义了一些category常量, 开发者也可以自定义category属性.Intent类的addCategory()方法为Intent添加Category属性, getCategories()方法用于获取Intent中封装的所有category.以下是Intent类中预定义的部分category:CATEGORY_HOME--表示目标activity必须是一个显示home screen的CATEGORY_LAUNCHER--表示目标activity可以作为task栈中的初始activity, 常与ACTION_MAIN配合使用;CATEGORY_GADGET--表示目标activity可以被作为另一个activity的一部分嵌入.Intent的Data属性data属性指定所操作数据的URI. data经常与action配合使用, 如果action为ACTION_EDIT, data的值应该指明被编辑文档的URI; 如果action为ACTION_CALL, data的值应该是一个以"tel:"开头并在其后附加号码的URI; 如果action为ACTION_VIEW, data的值应该是一个以"http: "开头并在其后附加网址的URI...Intent类的setData()方法用于设置data属性, setType()方法用于设置data的MIME类型, setDataAndType()方法可以同时设定两者. 可以通过getData()方法获取data属性的值, 通过getType()方法获取data的MIME类型.Intent的Extra属性通过Intent启动一个component时, 经常需要携带一些额外的数据过去. 携带数据需要调用Intent的putExtra()方法, 该方法存在多个重载方法, 可用于携带基本数据类型及其数组, String类型及其数组, Serializable类型及其数组, Parcelable类型及其数组, Bundle类型等. Serializable和Parcelable类型代表一个可序列化的对象, Bundle与Map类似,可用于存储键值对.Intent的Flag属性flag属性是一个int值, 用于通知android系统如何启动目标activity, 或者启动目标activity之后应该采取怎样的后续操作. 所有的flag都在Intent类中定义, 部分常用flag如下:FLAG_ACTIVITY_NEW_TASK--通知系统将目标activity作为一个新task的初始FLAG_ACTIVITY_NO_HISTORY--通知系统不要将目标activity放入历史栈中;FLAG_FROM_BACKGROUND--通知系统这个Intent来源于后台操作, 而非用户的直接选择...IntentFilter类IntentFilter类表示Intent过滤器, 大部分情况下, 每一个component都会定义一个或多个IntentFilter, 用于表明其可处理的Intent. 一般来说, component的IntentFilter应该在AndroidManifest.xml文件中定义. 定义的方法: 在&activity&, &receiver&, &service&元素中增加一个或多个&intent-filter&子元素. 如:&!-- 声明作为程序入口的Activity --&&activity android:name=".FirstActivity"&&intent-filter&&action android:name="android.intent.action.MAIN" /&&category android:name="android.intent.category.LAUNCHER" /&&/intent-filter&&/activity&IntentFilter与隐式Intentandroid系统处理隐式Intent时, 会比较Intent和IntentFilter的action, data, category属性, 如果以上3个属性全都相符的话, 则IntentFilter所属的component就可以作为目标组件的候选(存在多个符合条件的component时). 1. 测试action属性. intent最多只能定义1个action, 而filter可以定义1个或多个action. 通过action测试的条件为: filter定义了intent的action. 例如intent的action为"android.intent.action.MAIN", 则定义了"android.intent.action.MAIN"这个action的filter都能通过action测试(当然, filter还可以包含更多额外的action).如果filter没有定义action, 则这个filter将阻塞所有intent. 如果intent没有定义action, 那么只要filter定义了action就可以通过action测试. 2. 测试category属性. intent可以任意多个category, filter也可以任意个category. 通过category测试的条件为: filter定义了intent的所有category. 例如intent定义了"android.intent.category.DEFAULT"和"cn.xing.intent.category.UPLOAD"这2个category, 则定义了以上2个category属性的filter才能通过测试(当然, filter还可以包含更多额外的category).根据上面的规则, 如果一个intent没有定义category, 则所有filter都可以通过category测试. 但是有一种例外: 以startActivity(intent)方式启动一个activity时, 系统为会intent增加一个值为"android.intent.category.DEFAULT"的category, 这就意味着每一个期望通过category测试的activity, 都要在其filter中定义"android.intent.category.DEFAULT"(除了作为程序入口的activity).3. 测试data属性. intent最多只能定义1个data, filter则可以定义多个data.通过data测试的条件为:a. 如果intent没有指定data和data type, 则只有没有定义data和date type的filter才能通过测试;b. 如果intent定义了data没有定义data type, 则只有定义了相同data且没有定义date type的filter才能通过测试;c. 如果intent没有定义data却定义了data type, 则只有未定义data且定义了相同的data type的filter才能通过测试;d. 如果intent既定义了data也定义了data type, 则只有定义了相同的data和data type的filter才能通过测试.data属性是一个URI, URI中包含scheme, host, post和path, 典型的URI为:scheme://host:port/pathscheme, host, post和path都是可选的. 比较2个data时, 只比较filter中包含的部分. 比如filter的一个data只是指定了scheme部分, 则测试时只是比较data的scheme部分, 只要两者的scheme部分相同, 就视为"相同的data".01 Intent对象_图文_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
评价文档:
01 Intent对象
上传于||文档简介
&&a​n​d​r​o​i​d​学​习​基​础
大小:748.50KB
登录百度文库,专享文档复制特权,财富值每天免费拿!
你可能喜欢查看: 19806|回复: 9
精华0在线时间3 小时帖子主题UID64588积分2858技术分2380 资源分22 分享激情151 博客好友记录相册
高级工程师
知县, 积分 2858, 距离下一级还需 2142 积分
UID64588积分2858
还有class name在AndroidManifest.xml中定义的?还是指源代码.java中的类名?
我没有看到AndroidManifest.xml中哪里定义了class name和component name
1. 看ComponentName的构造函数,ComponentName(String pkg, String cls),一个是package name,一个是class name,这里的class可以是activity。
2. 匹配肯定是根据AndroidManifest.xml中定义的tag进行匹配的,你想啊,Android系统不能为了做一个匹配再动态分析一下你的程序里面定义了什么类吧。
精华33在线时间966 小时帖子主题UID6399积分42123技术分21272 资源分1172 分享激情9663 博客好友记录相册
高级架构师
UID6399积分42123
1. 看ComponentName的构造函数,ComponentName(String pkg, String cls),一个是package name,一个是class name,这里的class可以是activity。
2. 匹配肯定是根据AndroidManifest.xml中定义的tag进行匹配的,你想啊,Android系统不能为了做一个匹配再动态分析一下你的程序里面定义了什么类吧。
精华37在线时间20 小时帖子主题UID1682积分7498技术分2731 资源分120 分享激情135 博客好友记录相册
高级工程师
UID1682积分7498
Intent intent=new Intent();& &&&
& && && && && & intent.setComponent(new ComponentName(&com.android.calendar&, &com.android.calendar.LaunchActivity&));& &&&
& && && && && & startActivity(intent);& &
setComponent 中ComponentName为组件名,ComponentName(应用程序的包名,应用程序的Activity名);
setComponent多用于在一个应用中启动另外一个已安装的应用
总评分:&技术分 + 5&
精华0在线时间3 小时帖子主题UID64588积分2858技术分2380 资源分22 分享激情151 博客好友记录相册
高级工程师
知县, 积分 2858, 距离下一级还需 2142 积分
UID64588积分2858
谢谢,那就是component name是package name + activity name?
那class name呢?是.java中定义的class MyClass ???
精华0在线时间3 小时帖子主题UID64588积分2858技术分2380 资源分22 分享激情151 博客好友记录相册
高级工程师
知县, 积分 2858, 距离下一级还需 2142 积分
UID64588积分2858
谢谢,那就是component name是package name + activity name?
那class name呢?是.java中定义的class MyClass ???
精华37在线时间20 小时帖子主题UID1682积分7498技术分2731 资源分120 分享激情135 博客好友记录相册
高级工程师
UID1682积分7498
class name是指setClassName中的吗?
精华0在线时间3 小时帖子主题UID64588积分2858技术分2380 资源分22 分享激情151 博客好友记录相册
高级工程师
知县, 积分 2858, 距离下一级还需 2142 积分
UID64588积分2858
对,我指的就是setClassName
我看AndroidManifest.xml中一般都是写&activity android:name=&.TestActivity&/&或者&activity android:name=&TestActivity&/&,但是调用的时候却写
Intent intent = new Intent();
intent.setClassName(&com.tope.samples.intent.simple&,
& && && && && && &&&&com.tope.samples.intent.simple.TestActivity&);
startActivity(intent);
那它还是按照AndroidManifest.xml中定义的&activity android:name=&.TestActivity&&匹配的吗?
精华0在线时间3 小时帖子主题UID64588积分2858技术分2380 资源分22 分享激情151 博客好友记录相册
高级工程师
知县, 积分 2858, 距离下一级还需 2142 积分
UID64588积分2858
那setComponent和setClassName不是没区别了?
精华33在线时间966 小时帖子主题UID6399积分42123技术分21272 资源分1172 分享激情9663 博客好友记录相册
高级架构师
UID6399积分42123
看sdk咋说:
setClassName
Convenience for calling setComponent(ComponentName) with an explicit application package name and class name.
总评分:&技术分 + 5&
精华1在线时间5 小时帖子主题UID1362积分12885技术分10763 资源分6 分享激情102 博客好友记录相册
总督, 积分 12885, 距离下一级还需 7115 积分
UID1362积分12885
看了一下,
public Intent setClassName(String p,String c){
& & mComponent=new ComponentName(p,c);
及时将最新信息发给我
通过Email及时将最新的模板和服务发给我。
及时了解我们最新动态。关注我们的微博
官方微博: &&&14302人阅读
android 提高篇(35)
第一行:应用程序名称
第二行:应用程序包名
第三行:应用程序入口Activity名称
代码如下:
package com.hello.
import java.util.ArrayL
import java.util.C
import java.util.HashM
import java.util.I
import java.util.L
import java.util.M
import android.app.A
import android.app.ProgressD
import ponentN
import android.content.C
import android.content.I
import android.content.pm.ApplicationI
import android.content.pm.PackageM
import android.content.pm.ResolveI
import android.graphics.drawable.D
import android.net.U
import android.os.B
import android.os.B
import android.os.H
import android.os.M
import android.provider.S
import android.util.L
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.AbsListV
import android.widget.AdapterV
import android.widget.AbsListView.OnScrollL
import android.widget.AdapterView.OnItemClickL
import android.widget.BaseA
import android.widget.ImageV
import android.widget.ListV
import android.widget.SimpleA
import android.widget.TextV
public class SoftActivity extends Activity implements Runnable ,OnItemClickListener{
private List&Map&String, Object&& list =
private ListView softlist =
private ProgressD
private Context mC
private PackageManager mPackageM
private List&ResolveInfo& mAllA
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.software);
setTitle(&文件管理器&);
mContext =
mPackageManager = getPackageManager();
softlist = (ListView) findViewById(R.id.softlist);
pd = ProgressDialog.show(this, &请稍候..&, &正在收集软件信息...&, true,false);
Thread thread = new Thread(this);
thread.start();
super.onCreate(savedInstanceState);
* 检查系统应用程序,添加到应用列表中
private void bindMsg(){
//应用过滤条件
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
softlist.setAdapter(new MyAdapter(mContext, mAllApps));
softlist.setOnItemClickListener(this);
//按报名排序
Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
public void run() {
bindMsg();
handler.sendEmptyMessage(0);
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
pd.dismiss();
class MyAdapter extends BaseAdapter{
private List&ResolveInfo& resI
private ResolveI
private LayoutInflater infater=
public MyAdapter(Context context, List&ResolveInfo& resInfo) {
this.context =
this.resInfo = resI
infater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public int getCount() {
return resInfo.size();
public Object getItem(int arg0) {
return arg0;
public long getItemId(int position) {
public View getView(int position, View convertView, ViewGroup parent) {
// View view =
ViewHolder holder =
if (convertView == null || convertView.getTag() == null) {
convertView = infater.inflate(R.layout.soft_row, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
view = convertV
holder = (ViewHolder) convertView.getTag() ;
//获取应用程序包名,程序名称,程序图标
res = resInfo.get(position);
holder.appIcon.setImageDrawable(res.loadIcon(mPackageManager));
holder.tvAppLabel.setText(res.loadLabel(mPackageManager).toString());
holder.tvPkgName.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertV
/*convertView = LayoutInflater.from(context).inflate(R.layout.soft_row, null);
app_icon = (ImageView)convertView.findViewById(R.id.img);
app_tilte = (TextView)convertView.findViewById(R.id.name);
app_des = (TextView)convertView.findViewById(R.id.desc);
res = resInfo.get(position);
app_icon.setImageDrawable(res.loadIcon(mPackageManager));
app_tilte.setText(res.loadLabel(mPackageManager).toString());
app_des.setText(res.activityInfo.packageName+'\n'+res.activityInfo.name);
return convertV*/
//设定界面布局
class ViewHolder {
ImageView appI
TextView tvAppL
TextView tvPkgN
public ViewHolder(View view) {
this.appIcon = (ImageView) view.findViewById(R.id.img);
this.tvAppLabel = (TextView) view.findViewById(R.id.name);
this.tvPkgName = (TextView) view.findViewById(R.id.desc);
* 单击应用程序后进入系统应用管理界面
public void onItemClick(AdapterView&?& arg0, View arg1, int position, long arg3) {
ResolveInfo res = mAllApps.get(position);
//该应用的包名和主Activity
String pkg = res.activityInfo.packageN
String cls = res.activityInfo.
ComponentName componet = new ComponentName(pkg, cls);
Intent i = new Intent();
i.setComponent(componet);
startActivity(i);
software.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout xmlns:android=&/apk/res/android&
android:orientation=&vertical& android:layout_width=&fill_parent&
android:layout_height=&fill_parent&&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:drawSelectorOnTop=&false&
android:id=&@+id/softlist& /&
&/LinearLayout&
soft_row.xml
&?xml version=&1.0& encoding=&utf-8&?&
&LinearLayout
xmlns:android=&/apk/res/android&
android:id=&@+id/vw1&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&
android:orientation=&horizontal&&
&ImageView android:id=&@+id/img&
android:layout_width=&32dip&
android:layout_margin=&4dip&
android:layout_height=&32dip&/&
&LinearLayout
android:layout_width=&wrap_content&
android:layout_height=&wrap_content&
android:orientation=&vertical&&
&TextView android:id=&@+id/name&
android:textSize=&18sp&
android:textStyle=&bold&
android:layout_width=&fill_parent&
android:layout_height=&wrap_content&/&
&TextView android:id=&@+id/desc&
android:textSize=&14sp&
android:layout_width=&fill_parent&
android:paddingLeft=&10dip&
android:layout_height=&wrap_content&/&
&/LinearLayout&
&/LinearLayout&
最后别忘了添加权限:
&uses-permission android:name=&android.permission.READ_PHONE_STATE&/&
&uses-permission android:name=&android.permission.GET_TASKS&/&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:440281次
积分:4752
积分:4752
排名:第3785名
原创:70篇
译文:10篇
评论:185条
阅读:3194
阅读:25886}

我要回帖

更多关于 component name 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信