1

I have a main activity that launches a service to do a web search in the background and I would like the main activity to get an intent when the search is done.

In my main activity , I defined a BroadcastReceiver and an Intent Filter to listen to the "end of search" intent:

public class AgeRage extends Activity {


    // Listener to all results from background processes

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {  
            if (intent.getAction().equals(ImageSearchService.SEARCH_RESULT_ACTION)) {
0);
                Toast.makeText(context,"Got " + i + "results", Toast.LENGTH_SHORT).show();
            }
            else Toast.makeText(context,"unknown intent", Toast.LENGTH_SHORT).show();


        }
    };

    IntentFilter receiverFilter = new IntentFilter ();

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

        setContentView(R.layout.main);


        // Register to image search service messages

        receiverFilter.addAction(ImageSearchService.SEARCH_RESULT_ACTION);
        registerReceiver(receiver,receiverFilter);

       ...

In the service , I do the search and when it is done , I send an Intent:

public class ImageSearchService extends IntentService {

...

protected void onHandleIntent (Intent intent) {

... doing search ...

    Intent i = new Intent (this,AgeRage.class);
    i.setAction (SEARCH_RESULT_ACTION);
    i.putExtra(SEARCH_STATUS, (searchStatus ==SearchStatus.DONE) ? true:false);
    i.putExtra (SEARCH_RESULT_NUM, totalResultNum);
    i.putExtra (SEARCH_ID, searchID);
    sendBroadcast (i,null);
}

But, the main activity doesn't get the Intent. I know that the sendBroadcast is being called and the the receiver's OnReceive is not (checked with a debugger).

I assume that since I create the filter dynamically , I do not need to define a filter in the manifest file.

Am I doing something wrong ?

Thanks Isaac

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
Isaac K.
  • 11
  • 1
  • 2
  • 1
    Not sure of your question You are actually making the service interaction more complex than it needs to be. Since the Service is a Local Service you can talk to it directly and have it notify your activity when it completes without needing to broadcast an Intent and listen with a Receiver. Check out http://stackoverflow.com/questions/3197335/android-restful-api-service/3197456#3197456 – Robby Pond Mar 02 '11 at 23:21
  • Thanks , I just changed my code to work with a Receiver and it works perfectly. – Isaac K. Mar 03 '11 at 07:37
  • Good discussion on the topic in http://stackoverflow.com/questions/4233873/how-to-get-extra-data-from-intent-in-android – Alexey Vassiliev Jun 09 '12 at 16:02

1 Answers1

0

Ok. Well I just checked mine and we are doing it the same way, however ...

ImageSearchService.SEARCH_RESULT_ACTION

Try doing com.yourpackagename.ImageSearchSrvice.SEARCH_RESULT_ACTION

where SEARCH_RESULT_ACTION is a public static string variable. See if that helps.

I think it must be the naming of the ACTION. Also note that you might want to run tru the breakpoints and just check log. do intent.getAction() and print this out rather than checking inside the if statement. Just always print it out and see. Don't need to break inside a broacast receiver it will crash after a while.