android 监听系统时区变化,日期变化,时间变化

android 监听系统时区变化,日期变化,时间变化

原文地址:http://www.cnblogs.com/lotusve/archive/2012/05/30/2525371.html


1.  监听时区变化:

配置:

<receiver android:name="com.gionee.ui.DateTimeReceiver">
            <intent-filter>
                <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
            </intent-filter>
        </receiver>
复制代码
private static final String ACTION_TIMEZONE_CHANGED = Intent.ACTION_TIMEZONE_CHANGED;

@Override
    public void onReceive(Context context, Intent intent) {
        if (DBG) {
            Log.d(LOG_TAG, "---onReceive() start!---");
        }

        String action = intent.getAction();

        if (ACTION_TIMEZONE_CHANGED.equals(action)) {

            if (DBG) {
                Log.d(LOG_TAG, "---TIMEZONE_CHANGED!---");
            }

        }

        if (DBG) {
            Log.d(LOG_TAG, "---onReceive() end!---");
        }
    }
复制代码

2.  监听日期变化

配置:

<action android:name="android.intent.action.DATE_CHANGED" />
复制代码
private static final String ACTION_DATE_CHANGED = Intent.ACTION_DATE_CHANGED;

@Override
    public void onReceive(Context context, Intent intent) {
        if (DBG) {
            Log.d(LOG_TAG, "---onReceive() start!---");
        }

        String action = intent.getAction();

        if (ACTION_DATE_CHANGED.equals(action)) {

            if (DBG) {
                Log.d(LOG_TAG, "---DATE_CHANGED!---");
            }

        }
        
        if (DBG) {
            Log.d(LOG_TAG, "---onReceive() end!---");
        }
    }
复制代码

3.  监听时间变化

配置:

<action android:name="android.intent.action.TIME_SET" />
复制代码
private static final String ACTION_DATE_CHANGED = Intent.ACTION_DATE_CHANGED;
    private static final String ACTION_TIME_CHANGED = Intent.ACTION_TIME_CHANGED;

@Override
    public void onReceive(Context context, Intent intent) {
        if (DBG) {
            Log.d(LOG_TAG, "---onReceive() start!---");
        }

        String action = intent.getAction();

        if (ACTION_DATE_CHANGED.equals(action)) {

            if (DBG) {
                Log.d(LOG_TAG, "---DATE_CHANGED!---");
            }

        }

        if (ACTION_TIME_CHANGED.equals(action)) {

            if (DBG) {
                Log.d(LOG_TAG, "---TIME_CHANGED!---");
            }

        }
        
        if (DBG) {
            Log.d(LOG_TAG, "---onReceive() end!---");
        }
    }
复制代码

说明:

1.配置<action android:name="android.intent.action.TIME_SET" />,可同时监听日期,时间的变化。
2.单独监听时间变化的配置,目前不了解。
3.配置中还可<action android:name="android.intent.action.TIME_TICK" />,代码中可
复制代码
private static final String ACTION_TIME_TICK = Intent.ACTION_TIME_TICK;

if (ACTION_TIME_TICK.equals(action)) {

            if (DBG) {
                Log.d(LOG_TAG, "---TIME_TICK!---");
            }

        }
复制代码

此功能目前还不清楚用法。

猜你喜欢

转载自blog.csdn.net/hualizide/article/details/43731383