1

我想将一个字符串从一个应用程序发送到一个服务,我使用了绑定服务并且我已经实现了这个代码:

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

我的服务是:

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
    }

当我运行两个应用程序时,app1 显示“未绑定”:在这一行:

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

我应该怎么办?请帮我。提前致谢

4

1 回答 1

0

我用这个链接解决了我的问题:

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

于 2016-05-30T07:57:25.520 回答