如何在基于服务的Android应用程序中发送数据

时间:2013-03-15 10:30:00

标签: android aidl

我想使用AIDL文件将数据从一个应用程序A发送到其他应用程序B.我的appl 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);
        }
}

要访问上面的类,我将编写服务类,如下所示

public class EventService扩展Service {

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();
}

以上所有类都是服务器端。以下类是访问数据的客户端(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;

    }

我正在尝试从服务类访问数据但无法找到方法。任何人都可以告诉如何访问从服务到客户端应用程序的数据?

由于

0 个答案:

没有答案