Search code examples
javaandroidandroid-intentandroid-serviceandroid-service-binding

Bind service from a thread


Is it possible to bindService to a Service from a Thread?

I try to pass a String to the Service running, and the run a method to show a notification?

Any idea what's the correct way to do it?

Thanks in advance.


Solution

  • May you should try this way: First write your Service class.

    public class ShowNotifyService extends Service {
    
        private Messenger msg = new Messenger(new ShowNotifyHanlder());
    
        @Override
        public IBinder onBind(Intent arg0) {                
            return msg.getBinder();
        }
    
        class ShowNotifyHanlder extends Handler {
    
            @Override
            public void handleMessage(Message msg) {
                // This is the action 
                int msgType = msg.what;
    
                switch(msgType) {
                case SHOW_FIRST_NOTIFY: {
                    try {
                        // Incoming data
                        String data = msg.getData().getString("data");
                        Message resp = Message.obtain(null, SHOW_FIRST_NOTIFY_RESPONSE);
                        Bundle bResp = new Bundle();
                        bResp.putString("respData", first_notify_data);// here you set the data you want to show
                        resp.setData(bResp);
    
                        msg.replyTo.send(resp);
                    } catch (RemoteException e) {
    
                        e.printStackTrace();
                    }
                    break;
                }
                default: 
                    super.handleMessage(msg);
                }
            }
    }
    

    Then write your Activity class.

    public class TestActivity {
    
        ..
        private ServiceConnection sConn;
        private Messenger messenger;
        ..
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // Service Connection to handle system callbacks
                sConn = new ServiceConnection() {
    
                    @Override
                    public void onServiceDisconnected(ComponentName name) {
                        messenger = null;
                    }
    
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        // We are conntected to the service
                        messenger = new Messenger(service);
    
                    }
                };
        ...
                // We bind to the service
                bindService(new Intent(this, ShowNotifyService.class), sConn,
                        Context.BIND_AUTO_CREATE);
        ..
    
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    String val = edt.getText().toString();
                    Message msg = Message.obtain(null, ShowNotifyService.SHOW_FIRST_NOTIFY);
    
                    msg.replyTo = new Messenger(new ResponseHandler());
                    // We pass the value
                    Bundle b = new Bundle();
                    b.putString("data", val);
    
                    msg.setData(b);
    
                    try {
                        messenger.send(msg);
                    } catch (RemoteException e) {                    
                        e.printStackTrace();
                    }
    
                }
    
            });
        }
    
        // This class handles the Service response
        class ResponseHandler extends Handler {
    
            @Override
            public void handleMessage(Message msg) {
                int respCode = msg.what;
    
                switch (respCode) {
                    case ShowNotifyService.SHOW_FIRST_NOTIFY_RESPONSE: {
                        result = msg.getData().getString("respData");
                        //then you show the result data from service here
                    }
                }
            }
    
        }
    }
    

    I got this idea from here.