Home » Android » Broadcastreceiver – onReceive, extends, listener, manifest, pendingintent

Broadcastreceiver – onReceive, extends, listener, manifest, pendingintent

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have a BroadcastReceiver which is registered in Manifest, so it can run without the activity running, it’s important for the app.
It’s a timer app, and when it’s time, the receiver is called and makes some actions.
If the activity is running, and it’s time, I want to make some changes on the UI. So when the onReceive is called.
I tried to set a listener, but couldn’t make it work. Now I try to extend the receiver but it’s still not working.
Please help me.

What I already achieved:
– with a button click I start the timer, and it’s working even when the app is closed.
– when I start the timer, a black bar (clTimerInformation) appears with the text showing when the timer will end.

What I can’t achieve:
– when the timer ends (so when the onReceive is called), hide the black bar (clTimerInformation), but I can’t reach that from the broadcastreceiver. (That’s why I thought a Listener or extending the Receiver can make it, but I can’t make it work.)

BroadcastReceiver

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
            }
    }
}

Manifest

receiver android:name=".ProcessTimerReceiver"

Activity (Fragment)

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

}

How I tried with extending

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

}

}

UPDATE:

@Daniel Nugent: I updated the code with your suggestion by the code you linked, but it’s still not working. Could you please check it?

@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();
    }
Answers: