我正在寻找问题的答案,但在任何地方都找不到。
我有一个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");
}
等待您的帮助。先感谢您!
尝试使用广播:
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句