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种办 ...
随机推荐
- HDU 3571 N-dimensional Sphere
高斯消元,今天数学死了无数次…… #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- PhoneGap 开发笔记
1 调死调活都调不出来的情况下,可以考虑更换下phoneGap 版本,尽量用比较新的版本. 2 form submit 会返回 3 jquery mobile 的4个初始化事件 第一个触发的事件是mo ...
- JAVA GUI学习 - JTable表格组件学习_A ***
public class JTableKnow_A extends JFrame { public JTableKnow_A() { this.setBounds(300, 100, 400, 300 ...
- golang实现tcp接入服务器
接入服务器和后端业务服务其维持tcp连接,多个前端请求通过接入服务器访问后端业务服务器,接入服务器可以方便增加路由功能,维护多个业务服务器,根据消息ID路由到具体的业务服务器. 项目目录如下 simp ...
- HDU - 2276 Kiki & Little Kiki 2
Description There are n lights in a circle numbered from 1 to n. The left of light 1 is light n, and ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- 【转载】如何用Maven创建web项目(具体步骤)
使用eclipse插件创建一个web project 首先创建一个Maven的Project如下图 我们勾选上Create a simple project (不使用骨架) 这里的Packing 选择 ...
- block, inline和inline-block的区别
display:block元素的特点是:总是在新行上开始:高度,行高以及顶和底边距都可控制:宽度缺省是它的容器的100%,除非设定一个宽度<div>, <p>, <h1& ...
- 13. Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Square(hdu 1511)
题目描述: Problem Description Given a set of sticks of various lengths, is it possible to join them end- ...