1

here is my MainActivty I discard some method to be pasted here that makes my code shorter for u guys

public class MainActivity extends Activity {

private Button btn1,btn2;
private Binder binder;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    btn1 = (Button)findViewById(R.id.btn1);
    btn2 = (Button)findViewById(R.id.btn2);
    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, SecondService.class);
            bindService(intent, connection, BIND_AUTO_CREATE);
        }
    });

    btn2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();

            data.writeString("from activity: data");

            try {
                //something wrong here?
                binder.transact(0, data, reply, 0);

                String string = reply.readString();

                System.out.println("actvity   --->"+string);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
ServiceConnection connection = new ServiceConnection() {


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

    }

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

        MainActivity.this.binder = (Binder)binder;
    }
};

and here's my service

public class SecondService extends Service{

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

    return  new Mybinder();
}

class Mybinder extends Binder{
    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply,
            int flags) throws RemoteException {
        // TODO Auto-generated method stub

        System.out.println("code---->"+code);

        String string = data.readString();
        System.out.println("s---->"+string);

        reply.writeString("reply from :-->service");
        return super.onTransact(code, data, reply, flags);
    }
}}

Logcat below: enter image description here


it says line55: binder.transact(0, data, reply, 0); has some problems but I dont know why

any answer appreciate !

zxinyan
  • 81
  • 4

1 Answers1

0

Please write this code in onCreate

 Intent intent = new Intent();
 intent.setClass(MainActivity.this, SecondService.class);
 bindService(intent, connection, BIND_AUTO_CREATE);
Shoeb Siddique
  • 2,805
  • 1
  • 22
  • 42