2

In my project, when Service started, I send a broadcast:

Intent intent = new Intent("my.service.action");
intent.setPackage("com.my.project.test"); //only broadcast to my test project
getApplicationContext().sendBroadcast(intent);
Log.i("tag","broadcast is sent!");

In my test project AndroidTestCase, I start & bind the Service, which triggers the broadcast sending too. So, I decided to receive this broadcast also in my AndroidTestCase:

public class MyTestCase extends AndroidTestCase{ 
  ...
  @Override
  public void setUp() throws Exception{
    super.setUp();
     //This is working fine, I can see the broadcast is sent log in service
     bindToService() 

     //register broadcast receiver 
     IntentFilter filter = new IntentFilter("my.service.action");
     getContext().registerReceiver(mMyReceiver, filter);
  }

  public BroadcastReceiver mMyReceiver = new BroadcastReceiver(){

      @Override
      public void onReceive(Context context, Intent intent) {
      //BUT the broadcast sent in service is not received in my test case, why?
      Log.i(TAG, "Received in test case!");

     }  
 };
}

As you can see, I have registered a broadcast receiver in my AndroidTestCase of test project. Though the broadcast was sent in Service of my project, but it is not received. Why?

==========UPDATE===========

After I removed this line:intent.setPackage("com.my.project.test") when sending broadcast, my receiver in AndroidTestCase class is receiving the broadcast now.

But now, I am wondering why explicitly set package for intent blocks the receiver in test project, even though the package name I set is my test project. In AndroidManifest.xml of my test project, I have my package name definition:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.project.test"
   ...
Mellon
  • 37,586
  • 78
  • 186
  • 264
Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • `AndroidTestCase` is a `Service`, `Activity` or what? – XorOrNor Oct 18 '13 at 08:35
  • It is a general test case class in Android test framework: http://developer.android.com/reference/android/test/AndroidTestCase.html . The Service I mentioned is in my project not test project. – Mellon Oct 18 '13 at 08:37

1 Answers1

1
@Override
  public void setUp() throws Exception{
    super.setUp();
     //This is working fine, I can see the broadcast is sent log in service
     bindToService() 

     //register broadcast receiver 
     IntentFilter filter = new IntentFilter("my.service.action");
     getContext().registerReceiver(mMyReceiver, filter);
  }

Change To:

@Override
  public void setUp() throws Exception{
    super.setUp();

     //register broadcast receiver 
     IntentFilter filter = new IntentFilter("my.service.action");
     getContext().registerReceiver(mMyReceiver, filter);

   //This is working fine, I can see the broadcast is sent log in service
     bindToService();

  }

Take a look on this answer.Hope it will help u!!

Community
  • 1
  • 1
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
  • Actually, it is not because of the order in the code, I have intent.setPackage("com.my.project.test"), after I removed this line, it works fine, my receiver in AndroidTestCase class is receiving the broadcast now. But now, I am wondering why explicitly set the package for intent blocks the receiver... – Mellon Oct 18 '13 at 08:59