在模仿一个天气预报的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不起作用的更多相关文章

  1. android application类的用法

    android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...

  2. android的logcat详细用法

    Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命 令来查看和使用. 使用logcat命令 你可以用 logc ...

  3. 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)

    即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...

  4. Android开发中Bundle用法包裹数据(转)

    Android开发中Bundle用法包裹数据 Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据. SDK 里是这样描述: A mapping from String values ...

  5. 【转】Android各种Adapter的用法

    转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56)   阅读(143 ...

  6. android中broadcastreceiver的用法-代码中注册

    界面如下:     问题1:点击“解绑广播接收器“后再次点击”解绑广播接收器“后,程序崩溃,log信息如下: 08-04 05:04:35.420: E/AndroidRuntime(5521): F ...

  7. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  8. Android webView 正确的用法

    Android webView 正确的用法 引言: 我在网络找了几个例子,基本上都有问题,<Android疯狂讲义>13.4中的源代码也有问题.终于在官网找到正确的用法.点我. 基本用法: ...

  9. android之ListPreference的用法_PreferenceActivity用法

    首先,我们明确,preference是和数据存储相关的.        其次,它能帮助我们方便的进行数据存储!为什么这个地方一定要强调下方便的这个词呢?原因是,我们可以根本就不使用,我们有另外的N种办 ...

随机推荐

  1. 精简版LVCL,有空看看

    http://tothpaul.free.fr/sources.php?lvcl.lvcl1 http://synopse.info/forum/viewtopic.php?id=665

  2. Spring Boot简介

    Spring Boot简介 Spring Boot是为了简化Spring开发而生,从Spring 3.x开始,Spring社区的发展方向就是弱化xml配置文件而加大注解的戏份.最近召开的SpringO ...

  3. Flex4 设置combobox选项不可编辑

    近日做数据的增删改查,使用的flex4的ComboBox控件---> flex4中ComboBox其实就是TextInput的叠加 flex3中ComboBox其实就是Label的叠加 开始是使 ...

  4. Romantic(裸扩展欧几里德)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. 设计模式 - 适配器模式(adapter pattern) 具体解释

    适配器模式(adapter pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 适配器模式(adapter pattern): 将一个类的接 ...

  6. 五毛的cocos2d-x学习笔记01-创建项目

    终于准备开始学习cocos2d-x了.因为想和同学一起做游戏参加比赛,所以打算学习很热的Cocos2d-x.因为已经学习了C++,所以我想入门应该不是很困难.再加上官网有中文教程以及多不胜数的游戏开发 ...

  7. 「OC」构造方法和分类

    一.构造方法 (一)构造方法的调用 创建一个可用的对象:Person *p=[Person new]; new方法实际上是分为两步来创建一个对象: 1)使用+alloc方法来分配存储空间(返回分配的对 ...

  8. RPC框架——简单高效hessian的使用方式

    RPC(Remote Procedure Call Protocol) RPC使用C/S方式,采用http协议,发送请求到服务器,等待服务器返回结果.这个请求包括一个参数集和一个文本集,通常形成“cl ...

  9. beta分布

    http://blog.csdn.net/sweetrryy/article/details/6436358

  10. detain ssh server 设置(也许必须是root来安装?)

    ssh connection refused 处理方法 一般这种情况是 opens server 没安装 或 没启动 检查 openssh 是否安装 su 登录root账号,安装 openssh se ...