BroadcastReceiver 在两个服务之间不起作用

BroadcastReceiver doens't work between two Services

我的广播接收器在两个服务之间不工作..

这里是代码

[第一服务]

class ServiceHandler extends Handler
{
    @Override
    public void handleMessage(Message msg) {

       case Constants.MESSAGE_READ:
            Log.d("MainService_Handler", "Service - MESSAGE_READ: ");

            byte[] readBuf = (byte[]) msg.obj;
            // construct a string from the valid bytes in the buffer
            READ_DATA = readBuf;
            String strBuf = new String(readBuf, 0, msg.arg1);
            READ_STRING = strBuf;
            Intent intent = new Intent(MainService.this, InputHelperService.class);
            intent.setAction(InputHelperService.READ_MESSAGE);
            intent.putExtra("read", readBuf[1]);
            sendBroadcast(intent);

            //inputCommitment(readBuf[0]);

            break;

[第二次服务]

public class InputHelperService 扩展 InputMethodService {

public static String READ_MESSAGE = "com.example.eyelink.READMESSAGE";

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    Log.i("InputHelperService", "InputHelperService - onStartCommand()");

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("READ_MESSAGE");
    registerReceiver(messageReceiver, intentFilter);
    Log.i("InputHelperService", "InputHelperService - onStartCommand(): register the receiver");

    return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy(){
    Log.i("InputHelperService", "InputHelperService: onDestroy()");
    unregisterReceiver(messageReceiver);
}

...



public final BroadcastReceiver messageReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent){
        Log.i("InputHelperService", "InputHelperService: Broadcast onReceive()");
        if(intent.getAction().equals(READ_MESSAGE)){
            byte data = intent.getExtras().getByte("read");
            Log.i("InputHelperService", "InputHelperService: Broadcast onReceive() - " + data);
        }
    }
};

}

但是我的广播接收器没有响应。

怎么了?

这里:

intentFilter.addAction("READ_MESSAGE");

您正在使用不同的 Action 来准备 intentFilter 注册接收器和发送广播。使用与以下相同的操作字符串:

intentFilter.addAction(InputHelperService.READ_MESSAGE);

并发送广播为:

    Intent intent = new Intent(InputHelperService.READ_MESSAGE); 
    intent.putExtra("read", readBuf[1]);
    sendBroadcast(intent);