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"
...