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(转载请说明出处)

你可能感兴趣的:(Manifest)