Toast not displayed in bound service example using

2019-09-18 15:55发布

I'm following the example on android guide for bound service using messenger and running the following code snippet. I've modified the snippet slightly to have a hi and a bye button and the service should display the toast hi or bye depending on which button is pressed on UI.

But on running the example toast is not displayed. Although the message received log is printed on the UI.

What is the reason for this. The toast at onStart of Acticity is also not displayed.

public class MessengerService extends Service {
    public final static int MSG_SAY_HELLO = 1;
    public final static int MSG_SAY_BYE = 2;

    final Messenger mMessenger = new Messenger(new IncomingHandler());

    public static final String LOG_TAG = MessengerService.class.getName();

    @Override
    public IBinder onBind(Intent intent) {
        Toast.makeText(this, "binding", Toast.LENGTH_SHORT);
        return mMessenger.getBinder();
    }

    public class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            Log.d(LOG_TAG, "new msg arrived");
            switch (message.what) {
                case MSG_SAY_HELLO:
                    Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT);
                    break;
                case MSG_SAY_BYE:
                    Toast.makeText(getApplicationContext(), "bye!", Toast.LENGTH_SHORT);
                    break;
                default:
                    super.handleMessage(message);
            }
        }
    }
}

public class MyActivity extends Activity {

    Messenger mServiceMessenger = null;
    boolean mBound = false;

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName componentName, IBinder binder) {
            mServiceMessenger = new Messenger(binder);
            mBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mBound = false;
            mServiceMessenger = null;
        }
    };

    private void sayHello() {
        if(mBound){
            Message msg  = Message.obtain(null, MessengerService.MSG_SAY_HELLO, 0, 0);
            try{
                mServiceMessenger.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        } else {
            Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT);
        }
    }

    public void sayBye() {
        if(mBound) {
            Message msg  = Message.obtain(null, MessengerService.MSG_SAY_BYE, 0, 0);
            try {
                mServiceMessenger.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        } else {
            Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Toast.makeText(this, "binding", Toast.LENGTH_SHORT);
        bindService(new Intent(this, MessengerService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);
    }

    protected void onStop() {
        super.onStop();
        unbindService(mServiceConnection);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Button hi = (Button)findViewById(R.id.hello_world);
        Button bye = (Button)findViewById(R.id.bye_world);
        hi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sayHello();
            }
        });
        bye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sayBye();
            }
        });
    }

}

3条回答
smile是对你的礼貌
2楼-- · 2019-09-18 16:04

You are forgetting to call show() method Try this code snip.

Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show()
查看更多
男人必须洒脱
3楼-- · 2019-09-18 16:16

As far as i see you don't call the show method. Try to change the code like this:

Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show();
查看更多
Emotional °昔
4楼-- · 2019-09-18 16:22

You forgot to call the ".show()" method at the end of the Toast chain:

Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show();
查看更多
登录 后发表回答