从线程绑定服务

时间:2014-06-29 07:43:28

标签: java android android-intent android-service android-service-binding

是否可以从线程将服务绑定到服务?

我尝试将String传递给运行的Service,并运行一个方法来显示通知吗?

知道这样做的正确方法是什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

你应该这样试试: 首先写下你的Service课程。

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

然后写下你的Activity课程。

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
                }
            }
        }

    }
}

我从here得到了这个想法。