Search code examples
javaandroidaidl

How to use AIDL services for updating counter value?


I am new to android. I am trying using AIDL. What I am tryng is take initial value i.e 0 from the Textview and tryng to increment its value by calling a method defined in aidl file(using service.) But the TextView is not updating on button click. Kindly Help. Here is my code.

CounterService.aidl

package com.example.counter;

interface CounterService {

int updateCounter(in int var);
}

CounterServ.java (Exposing service)

package com.example.counter;

public class CounterServ extends Service {


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return new CounterService.Stub() {

        @Override
        public int updateCounter(int var) throws RemoteException {
            // TODO Auto-generated method stub
            return var + 1;
        }
    };
}

 @Override
  public void onDestroy() {
    super.onDestroy();

  }


    }

User Activity using service.

public class MainActivity extends Activity {

CounterService myservice;
AdditionServiceConnection connection;


class AdditionServiceConnection implements ServiceConnection {

    public void onServiceConnected(ComponentName name, IBinder Service) {
      myservice = CounterService.Stub.asInterface(Service);

      Toast.makeText(MainActivity.this, "Service connected", Toast.LENGTH_LONG)
          .show();
    }

    public void onServiceDisconnected(ComponentName name) {
      myservice = null;

      Toast.makeText(MainActivity.this, "Service connected", Toast.LENGTH_LONG)
          .show();
    }
  }

private void initService() {
    connection = new AdditionServiceConnection();
    Intent i = new Intent();
    i.setClassName("com.example",com.example.counter.MainActivity.class.getName());
    boolean ret = bindService(i, connection, Context.BIND_AUTO_CREATE);
          }


private void releaseService() {
    unbindService(connection);
    connection = null;
      }

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

    initService();

    Button update = (Button)findViewById(R.id.button1);

    update.setOnClickListener(new View.OnClickListener() {
        TextView value=(TextView)findViewById(R.id.textView1);
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            int val,res=-1;
            val=Integer.parseInt(value.getText().toString());

            try{

                res=myservice.updateCounter(val);

            }

            catch(Exception e)
            {e.printStackTrace();}

            value.setText(res);


        }



    });




        }


protected void onDestroy() {
    releaseService();
  }


    }

Solution

  • I got where I was lacking. I didnt register this service in the menifest file and therefore was not able to use it.