首页 > 解决方案 > 通过广播接收器检查时间根本不起作用

问题描述

我试着让我的广播检查时间是否改变,因为我想每分钟检查一次我放这个的时间

android.intent.action.TIMEZONE_CHANGED android.intent.action.TIME_SET android.intent.action.TIME_TICK

但它不起作用我能做什么。

标签: javaandroidbroadcastreceiverbroadcastandroid-broadcast

解决方案


//use these intent filter


   s_intentFilter = new IntentFilter();
    s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
    s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}

// 实现广播:

private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {                                                                                             
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(Intent.ACTION_TIME_CHANGED) ||
                    action.equals(Intent.ACTION_TIMEZONE_TICK)) {

            // your work
        }
    }
};

//注册接收者:

public void onCreate()/onResume {
    super.onCreate();
    registerReceiver(m_timeChangedReceiver, s_intentFilter);     
}

// 并取消注册:

public void onDestroy()/onPause {
    super.onDestroy();
    unregisterReceiver(m_timeChangedReceiver);     
}

推荐阅读