本博客致力于成为学习工作的参考文档
Intent:名为“意图”,科用于程序间的通讯,还可以应用于activity, service和broadcast receiver,我们可以用来启动一个Activity,用来发送并过滤广播,启动一个Service。相信我们最常用的就是用来启动一个Activity。
Intent分为:
1.显式Intent
2.隐式Intent
Intent由以下各个组成部分:
component(组件):目的组件
action(动作):用来表现意图的行动
category(类别):用来表现动作的类别
data(数据):表示与动作要操纵的数据
type(数据类型):对于data范例的描写
extras(扩展信息):扩展信息
Flags(标志位):期望这个意图的运行模式
隐式Intent:过滤器的作用,下面的代码意味着该Activity能接受的的action为“com.test.action”往下会介绍这个action。
<activity
android:name=".OtherActivity"
android:label="OtherActivity" >
<intent-filter>
<action android:name="com.test.action" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
1.启动一个Activity
Intent intent = new Intent();
//设置Intent的class属性,跳转到SecondActivity
intent.setClass(FirstActivity.this, SecondActivity.class);
startActivity(intent);
2.启动一个service
1)startService()
Intent intent=new Intent(MainActivity.this, ServiceDemo.class);
startService(intent);
2)bindService()
Intent intent = new Intent(MainActivity.this, BindService.class);
// 绑定Service
bindService(intent, conn, Service.BIND_AUTO_CREATE);
3.实现 BroadcastReceiver
String action = "com.example.receiver.ACTION";
Intent intent = new Intent();
intent.setAction(action);
sendBroadcast(intent);
在AndroidManifest.xml文件中注册BroadcastReceiver
<activity android:name="com.example.receiver.MyReceiver">
<intent-filter >
<action android:name="com.example.receiver.ACTION"/>
</intent-filter>
</activity>