android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用
2013-09-01 08:07
519 查看
在模仿一个天气预报的widget时候,用到了IntentFilter,感觉在manifest.xml注册的receiver跟用代码写registerReceiver()的效果应该是相同的,于是想证明一下,就写了如下一段程序:
MainActivity:
在manifest文件中注册如下:
布局文件如下:
总体说来很简单,就是通过两种方式注册了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
博客:/article/1349705.html(转载请说明出处)
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
博客:/article/1349705.html(转载请说明出处)
相关文章推荐
- android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用
- AndroidManifest.xml 之intent-filter 中 Action
- androidmanifest.xml中的activity/intent-filter/action/category
- android.intent.action.TIME_TICK在清单文件里注册不起作用
- android.intent.action.TIME_TICK在清单文件里注册不起作用
- android.intent.action.TIME_TICK在清单文件里注册不起作用
- 关于androidManifest.xml的概叙以及intent-filter的详细分析
- Android Manifest.xml中intent-filter的(data)
- Intent.ACTION_TIME_TICK的正确用法
- Android,配置Activity为启动Activity(AndroidManifest.xml,application,intent-filter,MAIN,LAUNCHER)
- android AndroidManifest.xml 多个android.intent.action.MAIN (
- 学习笔记-AndroidManifest intentfilter之category,action
- 显式与隐式Intent及AndroidManifest.xml文件中intent-filter参数说明
- 转:AndroidManifest.xml 中的intent-filter
- AndroidManifest.xml清单文件详解--intent-filter节点
- AndroidManifest.xml 之 android.intent.action.MAIN & android.intent.category.LAUNCHER
- AndroidManifest.xml 中的intent-filter
- Intent隐式启动 AndroidManifest.xml 中的intent-filter
- AndroidManifest.xml 中的intent-filter
- AndroidManifest Intent-Filter Action android:name属性