我有两个应用程序,即应用程序A和应用程序B.
A只是一个服务,它从BOOT COMPLETE开始,如果被销毁则重新启动。
A不断接收消息(通过串行连接,但这无关紧要)。
如果收到邮件但没有其他应用程序绑定到该服务,我不关心该邮件,它可能会丢失。
但是,如果接收到一些消息,并且假设B绑定了它,我希望B接收该消息。
所以,我尝试使用AIDL将消息从A传递给B,但我无法让它工作。我可能没有在这里做某事或没有正确使用它,所以我将在此感谢任何帮助。
我服务中的代码:
public class ExtProtocolService extends Service {
static RS232Comm serialComm;
public static final int BAUDRATE = 19200;
public static final int COM = 1;
public static Handler handler;
private Message message;
@Override
public IBinder onBind(Intent intent) {
return (new MessageBinder());
}
private static class MessageBinder extends IExtMessage.Stub {
@Override
public void receive(IExtMessageCallBack callback) {
new ReceiveThread(callback).
}
}
@Override
public void onCreate() {
serialComm = new RS232Comm(COM, BAUDRATE, 0, this);
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
message=msg;
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Protocol Service Started",
Toast.LENGTH_SHORT).show();
return START_STICKY;
}
public static class ReceiveThread implements Runnable {
int numCharsToRead = 2;
IExtMessageCallBack cb = null;
boolean ok;
ReceiveThread(IExtMessageCallBack cb) {
this.cb = cb;
}
@Override
public void run() {
try {
serialComm.Open();
ok = serialComm.Receive(numCharsToRead);
if (ok) {
cb.onSuccess();
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
RS232Comm serialComm 是我收到消息的类,主要代码在那里:
Message message=Message.obtain();
Bundle bundle=new Bundle();
bundle.putInt("command", command);
bundle.putInt("messageLength", messageLength);
bundle.putString("wholeData", wholeData);
message.setData(bundle);
Toast.makeText(context.getApplicationContext(), "Valid", Toast.LENGTH_LONG).show();
ExtProtocolService.handler.sendMessage(message);
return true;
正如您所见,我正在使用处理程序将消息传递回服务类。
我的AIDL文件是:
interface IExtMessage{
void receive(IExtMessageCallBack callback);
}
和:
interface IExtMessageCallBack{
void onSuccess();
void onFailure(String msg);
}
现在,应用B,它只是一个测试我的消息是否通过的主要活动:
package com.tfl.testmyservice;
public class MainActivity extends Activity implements ServiceConnection {
public static final String TAG = "YANIV";
private IExtMessage binding;
private Application appContext = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent implicit = new Intent(IExtMessage.class.getName());
List<ResolveInfo> matches = getPackageManager().queryIntentServices(
implicit, 0);
if (matches.size() == 0) {
Toast.makeText(getApplicationContext(),
"Cannot find a matching service!", Toast.LENGTH_LONG)
.show();
} else if (matches.size() > 1) {
Toast.makeText(getApplicationContext(),
"Found multiple matching services!", Toast.LENGTH_LONG)
.show();
} else {
Intent explicit = new Intent(implicit);
ServiceInfo svcInfo = matches.get(0).serviceInfo;
ComponentName cn = new ComponentName(
svcInfo.applicationInfo.packageName, svcInfo.name);
explicit.setComponent(cn);
bindService(explicit, this, Context.BIND_WAIVE_PRIORITY);
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
binding = IExtMessage.Stub.asInterface(binder);
}
@Override
public void onServiceDisconnected(ComponentName name) {
binding = null;
}
public void onEventMainTread(CallbackEvent event){
if(event.succeeded){
Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, event.msg, Toast.LENGTH_LONG).show();
}
}
IExtMessageCallBack.Stub cb=new IExtMessageCallBack.Stub() {
@Override
public void onSuccess() throws RemoteException {
new CallbackEvent(true, null);
}
@Override
public void onFailure(String msg) throws RemoteException {
new CallbackEvent(false, msg);
}
};
static class CallbackEvent{
boolean succeeded=false;
String msg=null;
public CallbackEvent(boolean succeeded,String msg) {
this.succeeded=succeeded;
this.msg=msg;
}
}
}
同样的AIDL也在应用B中。
我得到的输出是“找不到服务匹配!”
只是想指出服务本身就很有效。就在我使用AIDL尝试这个IPC时,事情变得有点混乱。
答案 0 :(得分:0)
看起来MainActivity中服务的操作字符串不正常。
您使用的是IExtMessage
的班级名称作为服务操作字符串。
Intent implicit = new Intent(IExtMessage.class.getName());
List<ResolveInfo> matches = getPackageManager().queryIntentServices(implicit, 0);
但通常与服务本身相关的字符串用于操作字符串,与IPC类无关。
您最好仔细检查App A的Android清单文件中服务ExtProtocolService
的intent过滤器中的操作字符串。您应该在查询意图时使用相同的字符串。
并检查服务是否已导出&#39;在清单文件中。