3

我想使用 AIDL 文件将数据从一个应用程序 A 发送到另一个应用程序 B。我的应用程序 A 如下所示,

public class LibValue  {       
    public static native int intFromJNI(int n);   
    static {
        System.loadLibrary("hello");
        System.out.println("LibValue : Loading library");
    }

我从 JNI 文件中获得价值到上面的类。上述类中使用 AIDL 服务发送另一个应用程序 B 的数据如下所示。

IEventService.aidl

interface IEventService {

   int intFromJNI(in int n);
}

为此,我编写了 IEventImpl.java 类

 public class IEventImpl extends IEventService.Stub{

        int result;

        @Override
        public int intFromJNI(int n) throws RemoteException {
            // TODO Auto-generated method stub

            System.out.println("IEventImpl"+LibValue.intFromJNI(n));
            return LibValue.intFromJNI(n);
        }
}

要访问上面的类,我会写如下服务类

公共类 EventService 扩展服务 {

public IEventImpl iservice;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub

    Log.i("EventService", "indside onBind");
    return this.iservice;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    this.iservice = new IEventImpl();

    Log.i("EventService", "indside OncREATE");
}

@Override
public boolean onUnbind(Intent intent) {
    // TODO Auto-generated method stub
    return super.onUnbind(intent);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub

    this.iservice = null;
    super.onDestroy();
}

以上所有类都是服务器端的。下面的类是用于访问数据的 Client(app) 类。

public class EventClient extends Activity implements OnClickListener, ServiceConnection{    

    public IEventService myService;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event_client); 

        button = (Button)findViewById(R.id.button1);
        this.button.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        if (!super.bindService(new Intent(IEventService.class.getName()),
                this, BIND_AUTO_CREATE)) {
            Log.w("EventClient", "Failed to bind to service");
            System.out.println("inside on resume");
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        super.unbindService(this);
    }


    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub


        this.myService = IEventService.Stub.asInterface(service);
        Log.i("EventClient", "ServiceConnected");




    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

        Log.d("EventClient", "onServiceDisconnected()'ed to " + name);
        // our IFibonacciService service is no longer connected
        this.myService = null;

    }

我正在尝试从服务类访问数据,但无法找到方法。谁能告诉如何从服务访问数据到客户端应用程序?

谢谢

4

0 回答 0