android之IntentFilter的用法_Intent.ACTION_TIME_TICK在manifest.xml不起作用

在模仿一个天气预报的widget时候,用到了IntentFilter,感觉在manifest.xml注册的receiver跟用代码写registerReceiver()的效果应该是相同的,于是想证明一下,就写了如下一段程序:MainActivity:public class MainActivity extends Activity { public static final i

大家好,又见面了,我是全栈君。

在模仿一个天气预报的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(转载请说明出处)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121401.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号