Broadcastreceiver - onReceive,extends,listener,manifest,pendingintent

时间:2017-10-22 15:11:32

标签: android android-broadcastreceiver

我有一个在Manifest中注册的BroadcastReceiver,因此它可以在没有运行活动的情况下运行,这对应用程序很重要。 它是一个计时器应用程序,当它的时候,接收器被调用并做出一些动作。 如果活动正在运行,并且是时候了,我想在UI上进行一些更改。所以当调用onReceive时。 我试图设置一个监听器,但无法使其工作。现在我尝试扩展接收器,但它仍然无法正常工作。 请帮帮我。

我已经取得的成就: - 点击按钮我启动计时器,即使关闭应用程序也能正常工作。 - 当我启动计时器时,会出现一个黑条(clTimerInformation),文本显示计时器何时结束。

我无法实现的目标: - 当计时器结束时(所以当调用onReceive时),隐藏黑条(clTimerInformation),但我无法从广播接收器到达。 (这就是为什么我认为听众或扩展接收器可以实现它,但我不能使它工作。)

广播接收器

public class ProcessTimerReceiver extends BroadcastReceiver {
    public static final String ACTION_ALARM_RECEIVER = "ACTION_ALARM_RECEIVER";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            if (ACTION_ALARM_RECEIVER.equals(intent.getAction())) {
                //some actions
            }
    }
}

清单

receiver android:name=".ProcessTimerReceiver"

活动(片段)

public class TimerTabbedActivity extends Fragment {

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_timer_tabbed, container, false);


    }

    private void getAlarmService() {
        alarmManager = (AlarmManager) getActivity().getSystemService(ALARM_SERVICE);
        helper.logD("Get alarm service");
    }

    private void createAlarm() {
        Intent alarmIntent = new Intent(getActivity(), ProcessTimerReceiver.class);
        alarmIntent.setAction(ProcessTimerReceiver.ACTION_ALARM_RECEIVER);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), REQUEST_CODE_ALARM, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        //...

        clTimerInformation.setVisibility(View.VISIBLE);
    }

}

我如何尝试扩展

public class TimerTabbedActivity extends Fragment implements ProcessTimerReceiver.ProcessTimerListener {

private LocalProcessTimerReceiver localProcessTimerReceiver;
    private IntentFilter localIntentFilter = new IntentFilter(ProcessTimerReceiver.ACTION_ALARM_RECEIVER);

private class LocalProcessTimerReceiver extends ProcessTimerReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            super.onReceive(context, intent);
            helper.logD("local receiver is working fine, thanks for asking");
        }
    }

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        localProcessTimerReceiver = new LocalProcessTimerReceiver();

    }

@Override
    public void onResume() {
        super.onResume();

        getActivity().registerReceiver(localProcessTimerReceiver, localIntentFilter);

}

}

更新:

@Daniel Nugent:我通过您链接的代码更新了您的建议代码,但它仍然无效。你能检查一下吗?

@Override
    public void onResume() {
        super.onResume();

        LocalBroadcastManager localBroadcastManager = 
        LocalBroadcastManager.getInstance(getActivity());
        localBroadcastManager.registerReceiver(localProcessTimerReceiver, localIntentFilter);

}

@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        localProcessTimerReceiver = new LocalProcessTimerReceiver();
    }

0 个答案:

没有答案