android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用
在模仿一个天气预报的widget时候,用到了IntentFilter,感觉在manifest.xml注册的receiver跟用代码写registerReceiver()的效果应该是相同的,于是想证明一下,就写了如下一段程序:
MainActivity:
public class MainActivity extends Activity { public static final int UPDATE = 000;
TextView xml;
TextView java;
int count = 0;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case UPDATE:
count ++;
xml.setText(count);
break; default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); IntentFilter javaFilter = new IntentFilter();
javaFilter.addAction("android.intent.action.TIME_TICK");
registerReceiver(receiver, javaFilter);
xml = (TextView) findViewById(R.id.xml);
java = (TextView) findViewById(R.id.java);
} BroadcastReceiver receiver = new BroadcastReceiver() { @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("HHmm");
java.setText(dateFormat.format(date));
}
}; protected void onDestroy() {
unregisterReceiver(receiver);
}; class Accept extends BroadcastReceiver { @Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Message message = handler.obtainMessage();
message.what = UPDATE;
handler.sendMessage(message);
} }
}
在manifest文件中注册如下:
<receiver android:name="jason.recevertext.Accept">
<intent-filter >
<action android:name="android.intent.action.TIME_TICK"/>
</intent-filter>
</receiver>
布局文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/xml"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <TextView
android:id="@+id/java"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> </LinearLayout>
总体说来很简单,就是通过两种方式注册了IntentFilter,然后接收到更新的intent之后更新主界面上的两个textview
结果java代码注册的IntentFilter很正常,xml注册的IntentFilter没有任何作用.
事实证明我的想法是错的,经过查询资料,发现:
在众多的Intent的action动作中,Intent.ACTION_TIME_TICK是比较特殊的一个,根据SDK描述:
Broadcast Action: The current time has changed. Sent every minute. You can not receive this through components declared in manifests, only by exlicitly registering for it withContext.registerReceiver()
意思是说这个广播动作是以每分钟一次的形式发送。但你不能通过在manifest.xml里注册的方式接收到这个广播,只能在代码里通过registerReceiver()方法注册。
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539/article/details/10815787(转载请说明出处)
android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用的更多相关文章
- android application类的用法
android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...
- android的logcat详细用法
Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命 令来查看和使用. 使用logcat命令 你可以用 logc ...
- 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)
即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...
- Android开发中Bundle用法包裹数据(转)
Android开发中Bundle用法包裹数据 Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据. SDK 里是这样描述: A mapping from String values ...
- 【转】Android各种Adapter的用法
转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56) 阅读(143 ...
- android中broadcastreceiver的用法-代码中注册
界面如下: 问题1:点击“解绑广播接收器“后再次点击”解绑广播接收器“后,程序崩溃,log信息如下: 08-04 05:04:35.420: E/AndroidRuntime(5521): F ...
- android 中uri.parse()用法
android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...
- Android webView 正确的用法
Android webView 正确的用法 引言: 我在网络找了几个例子,基本上都有问题,<Android疯狂讲义>13.4中的源代码也有问题.终于在官网找到正确的用法.点我. 基本用法: ...
- android之ListPreference的用法_PreferenceActivity用法
首先,我们明确,preference是和数据存储相关的. 其次,它能帮助我们方便的进行数据存储!为什么这个地方一定要强调下方便的这个词呢?原因是,我们可以根本就不使用,我们有另外的N种办 ...
随机推荐
- QT 判断当前焦点是否在某控件上(使用hasFocus函数,毕竟QT是DIRECTUI,所以这个Focus到底代表了什么呢?)
ui->pushButtonhasFocus()->hasFocus(); ui->treeWidget->hasFocus(); http://blog.csdn.net ...
- VS2010/MFC对话框:非模态对话框的创建及显示
非模态对话框的创建及显示 上一节讲了模态对话框及其弹出过程,本节接着讲另一种对话框--非模态对话框的创建及显示. 已经说过,非模态对话框显示后,程序其他窗口仍能正常运行,可以响应用户输入,还可以相互切 ...
- HDU1181【有向图的传递闭包】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 题意很简单. 有用并查集做的.我这里用传递闭包做. 有向图的传递闭包采用Floyd思想,可以判断 ...
- java实现冒泡排序,选择排序,插入排序,快速排序(简洁版)及性能测试
1.冒泡排序是排序里面最简单的了,但性能也最差,数量小的时候还可以,数量一多,是非常慢的. 它的时间复杂度是O(n*n),空间复杂度是O(1) 代码如下,很好理解. public void bubbl ...
- hive on spark的安装及问题
配置hive hive-site <property> <name>hive.metastore.uris</name> <value>thrift:/ ...
- 分支-15. 日K蜡烛图(15)
#include<iostream> using namespace std; int main(){ float o,h,l,c; while(cin>>o>>h ...
- Startup 和 Middleware(中间件)
Startup 和 Middleware(中间件) ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件) Startup Class 1.Startup Con ...
- hibernate 数据关联多对多 4.1
多对多,必须有一张关系表来维持关系 数据库student,teacher student_teacher 三张表 但是在pojo中只需要建立student和teacher两个类,除非关系表也代表某种业 ...
- Tomcat中配置自定义404错误页面
404,50x这种错误经常遇到. 如果%CATALINA_HOME%\conf\web.xml和具体应用中都有设置%CATALINA_HOME%\webapps\ROOT\WEB-INF\web.xm ...
- cocos2d-x游戏开发系列教程-超级玛丽10-怪物与马里奥冲突检测
在超级玛丽游戏中,马里奥在移动,怪物也在移动,当他们遇见时,需要判断是马里奥身亡还是怪物身亡. 这个判断的代码在怪物类的检测函数实现中. 比如蘑菇怪的冲突检测函数: bool CMMonsterMus ...