android.intent.action.TIME_TICK

来源:互联网 发布:fedora和centos哪个好 编辑:程序博客网 时间:2024/06/03 16:47

今天做一个显示系统时间的模块,需要监听时间的走动。于是新建了一个继承BroadcastReceiver的类:

public class TimeTickReceiver extends BroadcastReceiver {@Overridepublic void onReceive(final Context context, Intent intent) {String action = intent.getAction();if (action.equals(Intent.ACTION_TIME_TICK)) {ClockView.updateTime();} }}

想当然的在AndroidManifest.xml文件里注册监听:

<receiver android:name="com.example.weather.TimeTickReceiver" >        <intent-filter>            <action android:name="android.intent.action.TIME_TICK" />        </intent-filter></receiver>

结果无法监听时间的走动。查阅了Google的API,发现问题所在:




android.intent.action.TIME_TICK是一个受保护的Intent,只能被系统发出。它不能通过在AndroidManifest.xml文件中注册来接收广播,只能通过

Context.registerReceiver()明确注册。我在onCreate()方法中注册android.intent.action.TIME_TICK如下:

IntentFilter filter = new IntentFilter();filter.addAction(Intent.ACTION_TIME_TICK);receiver = new TimeTickReceiver();registerReceiver(receiver, filter);

监听时间走动成功。


0 0