我有兩個應用,應用A和應用程序B.使用從服務將消息發送到不同的應用程序AIDL
A是僅有的服務,其中,如果銷燬BOOT COMPLETE並重新啓動開始。
甲不斷接收消息(通過串行連接,但是這是不相關的)。
如果收到消息,但沒有任何其他應用程序綁定到服務,我不關心消息,它可能會丟失。
但是,如果接收到某條消息並假設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時,事情會變得有點混亂。