Intent用法总结
来源:互联网 发布:拜乐生物杀蟑饵剂 知乎 编辑:程序博客网 时间:2024/04/18 17:23
activities,services, broadcast receivers均是由intent开启的.
但是这三种用法不会重叠。
在帮助文档中是这样介绍的:
· An Intentobject is passed to Context.startActivity() orActivity.startActivityForResult() to launch an activity or get an existing activity to dosomething new. (It can also be passed to Activity.setResult() to return information to the activity that called startActivityForResult()
· An Intentobject is passed to Context.startService() to initiate a service or deliver new instructions to anongoing service. Similarly, an intent can be passed to Context.bindService() to establish a connection between the calling componentand a target service. It can optionally initiate the service if it's notalready running.
· Intentobjects passed to any of the broadcast methods (such as Context.sendBroadcast(), Context.sendOrderedBroadcast(), orContext.sendStickyBroadcast()) are delivered to all interested broadcast receivers.Many kinds of broadcasts originate in system code.
第三种用法,传递给broadcast method,例如Context.sendBroadcast(), Context.sendOrderedBroadcast(), orContext.sendStickyBroadcast()
Android系统代码中有很多这种用法,我用Google代码搜索随意找了下,整理如下:
我自己找的,不要鄙视,有观点错误还请各位指教:
定义:
frameworks/base.git›core›java›android›content›Intent.java
/**
* Broadcast Action: The time was set.
*/
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
public static final String ACTION_TIME_CHANGED = "android.intent.action.TIME_SET";
发送出去:
packages/apps/Settings.git›src›com›android›settings›DateTimeSettings.java
private void timeUpdated() {
Intent timeChanged = new Intent(Intent.ACTION_TIME_CHANGED);
sendBroadcast(timeChanged);
}
接收:
packages/apps/Settings.git›src›com›android›settings›DateTimeSettings.java
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateTimeAndDateDisplay();
}
};
mIntentReceiver的register和unregister
register:
packages/apps/Settings.git›src›com›android›settings›DateTimeSettings.java 的onResume()中
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
registerReceiver(mIntentReceiver, filter, null, null);
unregister:
packages/apps/Settings.git›src›com›android›settings›DateTimeSettings.java 的onResume()中
protected void onPause() {
super.onPause();
unregisterReceiver(mIntentReceiver);
。。。。
}
通常情况下BroadcastReceiver都是在onResume时被register,在onStop时被unregister(我自己认为的,呵呵)
当然BroadcastReceiver也可以自己独立不写在Activity里面,这时就会在AndroidManifest.xml里面有定义类似
/development.git›samples›ApiDemos›AndroidManifest.xml
<receiver android:name=".appwidget.ExampleBroadcastReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.ACTION_TIMEZONE_CHANGED" />
<action android:name="android.intent.ACTION_TIME" />
</intent-filter>
</receiver>
其实上面写在activity的方式也需要在xml里面加对应的intetfilter,但是上面那段代码动态的写在activity里面了,所以xml里面就没有写
接收:
development.git›samples›ApiDemos›src›com›example›android›apis›appwidget›ExampleBroadcastReceiver.java
public class ExampleBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
…………
}
另外我自己有写了一些关于intent用于service的一些用法的例子(没力气贴代码了,感兴趣的下工程看),包括intent用于broadcast的一些用法,将它们都写在一个工程里面了,方便查阅,基本文档中提到的用法我都有试。Intent用于activity的方法比较常见,所以我就没有写了。有兴趣的可以下载整个工程看,我是小小菜鸟请勿鄙视。
地址:http://download.csdn.net/source/2296676
- Intent用法总结
- Intent用法总结
- Intent的用法总结
- Android Intent用法总结
- Intent用法简单总结
- android intent 用法总结
- Intent 用法全面总结
- Android Intent 用法总结
- Android:Intent用法总结
- Intent用法总结
- Intent 用法总结
- Intent 用法总结
- Intent的用法总结
- intent用法总结
- Android Intent 常见用法总结
- Android Intent的用法总结
- Android Intent用法全面总结
- Android Intent 用法全面总结
- 字符串的复制(一)
- 百度的青春期
- SQL server子查询
- 递归法求阶乘
- ubuntu不能升级和安装软件的解决办法(E: Sub-process /usr/bin/dpkg returned an error code (1) )
- Intent用法总结
- Socket连接池设计
- Socket编程中用send发送结构体
- openGL CG 系列教程
- UltraEdit打开文件总弹出dos格式 && && 显示函数列表 && V22注册方式
- 我现在的工作策略
- sp_executesql 用法
- 让人无语的Access关键字
- 如何使菜单栏变灰 MFC