AccesibilityService和Activity之间的通信

时间:2013-12-15 20:40:07

标签: android

我正在寻找我的问题的答案,我还没有找到它。

我运行了AccesibilityService,我想将信息发送到MainActivity。

我一直在尝试使用Messenger类,但我不能。

任何人都可以帮助我吗?

我已将此代码放在mainactivity中:

class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) { 
        switch (msg.what) {
        case NoficationService.MSG_ENCENDER:
            // Envía "1".
            String message = "1";
            mApp1.sendData(message);
            break;
        default:
            super.handleMessage(msg);
        }
    }
}

在MainActivity中我有这段代码:

try {
            Message msg = Message.obtain(null, NoficationService.MSG_ENCENDER, 0, 0);
            msg.replyTo = mMessenger;
            mService.send(msg);
            Log.d(tag, "Enviado Msg");
        } catch (RemoteException e) {
            Log.d(tag, "Excepción Msg");
        }

等待你的帮助。提前谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用广播:

public final static String BROADCAST_ACTION = "com.packegeName";

1)在您的活动中注册广播:

BroadcastReceiver  br = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {
        // proccess messages
}
}


IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION); 
registerReceiver(br, intFilt);

2)从AccesibilityService发送广播

Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
intent.putExtra(MainActivity.PARAM_STATUS, MainActivity.STATUS_FINISH);
intent.putExtra(MainActivity.PARAM_RESULT, time * 100);
sendBroadcast(intent);