1

I want to send a string from an app to a service, I used bind service and I have implemented this code:

package pref.com.app1;
public class MainActivity extends AppCompatActivity {


    Messenger mService = null;
    /** Flag indicating whether we have called bind on the service. */
    boolean mIsBound;



    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {

    Toast.makeText(getApplicationContext(), "IncomingClient--:"+msg.arg1, Toast.LENGTH_LONG).show();
        }
    }

    /**
     * Target we publish for clients to send messages to IncomingHandler.
     */
    final Messenger mMessenger = new Messenger(new IncomingHandler());
  private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,
                                       IBinder service) {
            Toast.makeText(getApplicationContext(), "sn:"+service.toString(),
                    Toast.LENGTH_SHORT).show();

            mService = new Messenger(service);

            try {
                Message msg = Message.obtain(null,
                       1);
                msg.replyTo = mMessenger;
                mService.send(msg);

                // Give it some value as an example.
                msg = Message.obtain(null,
                        1, this.hashCode(), 0);
                mService.send(msg);
            } catch (RemoteException e) {

                  Toast.makeText(getApplicationContext(), "catch1--:"+e.toString(),
                Toast.LENGTH_SHORT).show();
            }


        }

        public void onServiceDisconnected(ComponentName className) {

            mService = null;
            Toast.makeText(getApplicationContext(),"Handler-Disconnect---",
                    Toast.LENGTH_SHORT).show();
        }
    };


    public   void doBindService() {

        Intent intentForMcuService = new Intent();
        intentForMcuService.setComponent(new ComponentName("com.example.newpro", "com.example.newpro.MainActivity"));
        if (getApplicationContext().bindService(intentForMcuService, mConnection,Context.BIND_AUTO_CREATE
        )){
            Toast.makeText(getApplicationContext(),"binded",
                    Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getApplicationContext(),"not binded",
                    Toast.LENGTH_SHORT).show();
        }

        mIsBound = true;
        Toast.makeText(getApplicationContext(), "binding-client",
                Toast.LENGTH_SHORT).show();
    }
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


         Button sosBtn = (Button) findViewById(R.id.button);
        sosBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                doBindService();
            }
        });
    }

and my service is:

package com.example.newpro;
public class MainActivity extends Service {

final android.os.Messenger mMessenger = new android.os.Messenger(new IncomingHandler());

@SuppressLint("HandlerLeak")
class IncomingHandler extends Handler {
    @Override
    public void handleMessage(android.os.Message msg) {

        Toast.makeText(getApplicationContext(), "IncomingServer-:"+msg.arg1, Toast.LENGTH_LONG).show();

    }
}

@Override
public IBinder onBind(Intent intent) {
    return mMessenger.getBinder();
}

@Override
public void onStart(Intent intent, int startId) {
    //my code
    }

when I run two application ,app1 shows "not binded": in this line:

Toast.makeText(getApplicationContext(),"not binded",
                        Toast.LENGTH_SHORT).show();

what should I do? please help me. thanks in advance

1 Answers1

0

I solved my problem with this link:

http://www.techotopia.com/index.php/Android_Remote_Bound_Services_%E2%80%93_A_Worked_Example