Search code examples
androiduser-interfaceandroid-activitybroadcastreceiver

Updating Activity from separate broadcastreceiver, how?


I am writing an application which catches media-button-clicks from a bt-headset. However, since my receiver is stated in the manifest-file, I am suddenly not in control of my activity any more, and what I really want to do is to "press" a software-button on the activity when somebody presses the headset button.

I've tried several solutions but have not got anything to work. Here is my current state

package com.siriforreq.activities;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MediaButtonReceiver extends BroadcastReceiver{
    private boolean gotMessage = false;

    public MediaButtonReceiver(){
        super();
    }

    public boolean isMessage(){
        return this.gotMessage;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("IN ON RECEIVE1");
         if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {


                System.out.println("GOT MEDIA BUTTON");
                Intent startBroadcastIntent = new Intent(context, NetworkActivity.class);
                startBroadcastIntent.setAction("com.siriforerq.activities.ACTION_LOL");
                startBroadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
                System.out.println("...sending broadcast");
                context.sendBroadcast(startBroadcastIntent); 



         }
    }


    }

In the above receiver, I do catch the media button. But where do I go from now to update my Activity? What I am trying to do right now is to send another custom broadcast and catch that in another receiver within the activity, but it does not seem to work. Here is my Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_network);


    mMediaReceiverCompName = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    IntentFilter btCommunicationFilter = new IntentFilter();
    btCommunicationFilter.setPriority(1000);
    btCommunicationFilter.addAction("com.siriforerq.activities.ACTION_LOL");
    catchButtonEvent = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            System.out.println("Got broadcast yes");
            //startTalkInteraction(); <- this is the method I want to    call in the activity           
        }       
    };
    registerReceiver(catchButtonEvent, btCommunicationFilter);

}

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

    mAudioManager.registerMediaButtonEventReceiver(mMediaReceiverCompName); 
    socketNetworkHelper = SocketNetworkHelper.getSocketNetworkHelper(ip, port);
    setButtonStatus(true);

}

In the above anonymous receiver, the broadcast that is being sent from the MediaButtonReceiver never gets called and here is my problem right now. Should I think in another way or why doesn't my onReceive in the anonymous receiver-class trigger?


Solution

  • As it can be seen in the docs you are using the constructor Intent(class,context) which creates an Intent for a specific component. Instead you should construct your Intent like this :

    Intent startBroadcastIntent = new Intent();
    startBroadcastIntent.setAction("com.siriforerq.activities.ACTION_LOL");
    startBroadcastIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);