不从onBind()调用IBinder类

时间:2015-04-30 14:47:22

标签: android service ipc aidl

我有服务和客户。 该服务正在后台运作。 我想要的是,当我的服务收到某个消息时,它将被传递给客户端(或任何选择绑定到服务的客户端) 我尝试使用aidl实现双向IPC 我能够绑定,并且onBind被调用 但是,我试图返回的IBinder对象不会被调用。

我的AIDL文件:

package com.tfl.extprotocolservice;

interface ICallBackMessage{
void onMessageReceived(int command);
}

和,

package com.tfl.extprotocolservice;
import com.tfl.extprotocolservice.ICallBackMessage;



interface IExtMessage{
    void getMessage(ICallBackMessage cb);
}

我的客户:

package com.tfl.testmyservice;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import com.tfl.extprotocolservice.ICallBackMessage;
import com.tfl.extprotocolservice.IExtMessage;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity implements ServiceConnection  {
    public static final String TAG = "YANIV";
    private IExtMessage binding=null;
    private Message message;
    private Application appContext = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // TODO Auto-generated method stub
        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{
            Toast.makeText(getApplicationContext(), "Found the Protocol Service", Toast.LENGTH_LONG).show();
            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_AUTO_CREATE);



        }

    }


    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Log.d("TEST", "Service Connected");
        binding=IExtMessage.Stub.asInterface(service);

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        binding=null;

    }


    ICallBackMessage.Stub cb=new ICallBackMessage.Stub() {

        @Override
        public void onMessageReceived(int command) throws RemoteException {
            Log.d("TESTSERVICE","command received "+command);
            Toast.makeText(getApplicationContext(), "Command "+command, Toast.LENGTH_LONG).show();

        }
    };




    }

以及我服务中的相关方法:

@Override 
public IBinder onBind(Intent intent) {

        return (new ExtMessageBinder());


}


private static class ExtMessageBinder extends IExtMessage.Stub {

    @Override
    public void getMessage(ICallBackMessage cb) throws RemoteException {

        cb1 = cb;

    }

}

调用onBind(),但永远不会到达ExtMessageBinder。 可能是什么问题呢 ?

感谢

0 个答案:

没有答案