广播接收机检查时间根本不起作用

时间:2019-02-07 11:08:25

标签: java android broadcastreceiver broadcast android-broadcast

我尝试让广播检查时间是否更改,因为我想每隔一分钟检查一次时间

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

但是行不通,我该怎么办。

1 个答案:

答案 0 :(得分:0)

//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);     
}