私のプロジェクトでは、Service
開始時にブロードキャストを送信します。
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!");
私のテストプロジェクト では、ブロードキャスト送信もトリガーAndroidTestCase
する を開始してバインドします。Service
それで、私はこのブロードキャストを私の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!");
}
};
}
ご覧のとおりAndroidTestCase
、テスト プロジェクトにブロードキャスト レシーバーを登録しました。Service
私のプロジェクトのブロードキャストが送信されましたが、受信されません。なんで?
==========更新===========
この行を削除した後、ブロードキャストを送信すると、クラスintent.setPackage("com.my.project.test")
のレシーバーがブロードキャストを受信するようになりました。AndroidTestCase
しかし今、私が設定したパッケージ名が my test projectであるにもかかわらず、明示的に設定されたパッケージがtest projectの受信者をブロックする理由が不思議です。テスト プロジェクトのAndroidManifest.xmlには、パッケージ名の定義があります。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.project.test"
...