通过广播接收器检查时间不能工作

问题描述 投票:1回答:1

我尽量让我的广播检查,如果时间改变或不是因为我要检查的时间每一分钟我把这个

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

但它不工作,我能做些什么。

java android broadcastreceiver broadcast android-broadcast
1个回答
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);     
}
© www.soinside.com 2019 - 2024. All rights reserved.