0

I have following questions

  1. Do i need to create a simple file with .aidl extension ?
  2. I read that a corresponding .java file will be created, but in my case when i created a .aidl file;i couldn't find any such .java file
  3. is there any particular folder where i need to create this .aidl file.
user986959
  • 620
  • 2
  • 7
  • 22

4 Answers4

0

Create a .aidl file for objects to be passed through IBinder. You should create the .aidl file in the same package as the Java object that it defines.

Google Developer, Android Interface Definition Language (AIDL)

mach
  • 8,315
  • 3
  • 33
  • 51
0

Here is my answer:

  1. If you need create a Service, you need create a .aidl file to define the API first.
  2. If you have create a .aidl file, the ADT will automatically create the correspoding .java file in the gen/your-package-name folder.
  3. No. After you create the file, you should follow the steps:

    a. define the API in .aidl

    b. create your service which extends from android Service class

    c. create a instance which implements XXX.Stub (XXX is your .aidl filename)

    d. bind your service

Hope it helps.

aarontang
  • 229
  • 3
  • 7
0
/********************Application 1 Isum.aidl*****************
package com.example.Isum;

interface Isum{

int sum(int a, int b);

}

/***********************Application 1*********Don't do anything in main
/********create service class*********remote service
public class RemoteService extends Service{



    @Override
    public IBinder onBind(Intent intent) {


        return mBinder;
    }
    @Override
    public void onCreate(){
        super.onCreate();
    }
    @Override
    public void onDestroy(){
        super.onDestroy();
    }
private final Isum.Stub mBinder=new Isum.Stub() {

        @Override
        public int sum(int a, int b) throws RemoteException {


            int c=a+b;
            return c;
        }
    };

**********************Application 2  Main activity*********
public class MainActivity extends Activity {

    private Isum msum;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button btnSum=(Button)findViewById(R.id.btnResult);


        btnSum.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                bindService(new Intent("com.example.serviceprovider.RemoteService"),
                        mConnection, Context.BIND_AUTO_CREATE);

                        EditText FirstNo=(EditText)findViewById(R.id.ETfirstNo);
                        EditText SecondNo=(EditText)findViewById(R.id.ETSecondNo);

                        int a=Integer.parseInt(FirstNo.getText().toString());
                        System.out.println("###########"+a);
                        int b=Integer.parseInt(SecondNo.getText().toString());

                        try {

                            int c=msum.sum(a, b);
                            System.out.println("***********************"+c);


                            Toast.makeText(MainActivity.this, c, Toast.LENGTH_LONG).show();



                        } catch (Exception e) {

                            e.printStackTrace();
                        }
            }
        });


    }

    private ServiceConnection mConnection=new ServiceConnection(){

        @Override
        public void onServiceConnected(ComponentName arg0, IBinder arg1) {

            msum=Isum.Stub.asInterface(arg1);

        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            msum=null;


        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
/****************include this in mainfest file


 <service android:name="RemoteService">
            <intent-filter>
                <action android:name="com.example.serviceprovider.RemoteService"/> 
                 </intent-filter>
           </service>
0
public void onStart(){
    super.onStart();
    Intent intent=new Intent(MainActivity.this, RemoteService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}


public void onServiceConnected(ComponentName className, IBinder service) {
    mIsum=Isum.Stub.asInterface((IBinder)service);
    mBound=true;
    }