Intent(意图)是一个将要执行操作的抽象描述,对于运行时绑定不同的组件是很方便的。
传递Intent到不同组件的机制是互不相同的。
Activity,Service,Broadcast Receiver——是通过intent传递信息的,而另外一个组件ContentProvider本身就是一种通信机制,不需要通过intent。
Activity: startActivity(Intent intent);startActivityForResult(Intent intent,int requestCode)
Service:startService(Intent intent)
bindService(Intent intent,ServiceConnection conn,int flags)
BroadCasts :sendBroadcasts(Intent intent)
sendOrderedBroadcasts(Intent intent,StringreceiverPermission,BroadcastReceiver resultReceiver)
Intent相关组件:
component:目的组件
Action:用来体现意图的行动
category:用来体现动作的类别
data:表示与动作要操控的数据
type:对data范例的描写
extras:扩展信息
显式Intent:很简单,不细说;
Intent intent =new Intent(MainActivity.this,TextViewActivity.class);//TextViewActivity为另一个事件
//显示2,intent.setClass(MainActivity.this,TextViewActivity.class);
//显示3,intent.setClassName(MainActivity.this,"路径名“);
//显示4,intent.setComponent(new Component(MainActivity.this,"路径名“)); 其实和第一种一样都是调用了Component
//隐式1 intent.setaction("在AndroidMainfest里定义的Androidname”);记得把category也改一下,LAUCNCH改DEAFULT
//可以跳转到其他应用的act
数值传递:Intent putExtra("username",name_str);//实际上是new了一个Bundle完全可以这么代替:
Bundle bundle=new Bundle();
bundle.putString("name","asd");
intent.putExtra(bundle);
隐式Intent:
过滤器:
<intent-filter>
<action^^/>//指定什么动作
<category^^/>//指定以什么方式来执行动作
<data^^/>//向action提供要操作的数据
</intent-filter>
Component name 组件名称
//ComponentName componentname=new Component(“路径名”,"路径名“);
intent.setComponent(new Component(MainActivity.this,"路径名“));
Action动作
有指定的常量:
ACTION_DIAL 调用拨号面板
ACTION_VIEW 指定显示 ;ACTION_CALL指定打电话;ACTION_SENDTO指定发信息
Data数据
intent.setAction(intent.ACTION_VIEW);
intent.setData(Uri.parse("http://^^");
注意:打电话还要加权限:<users-permission ^^^/>
Category 种类
CATEGORY_DEFAULT:指定默认Activity CATEGORY_LAUNCH 指定启动时的activity
CATEGORY_HOME返回系统桌面
intent.setAction(intent.ACTION_MAIN);
intent.addCategory(intent.CATEGORY_HOME);
Extras附加信息
Flags标志
intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY);//不保存历史
详情请看:添加链接描述
1530




被折叠的 条评论
为什么被折叠?



