Android 使用messenger的绑定服务示例中未显示Toast
下面是使用messenger的android绑定服务指南示例,并运行以下代码片段。 我稍微修改了这个片段,使它有一个hi和bye按钮,服务应该显示toast hi或bye,具体取决于UI上按下的按钮 但在运行示例时,不会显示toast。尽管收到的消息日志打印在UI上 原因何在?。活动开始时的祝酒词也不显示Android 使用messenger的绑定服务示例中未显示Toast,android,android-toast,android-service-binding,Android,Android Toast,Android Service Binding,下面是使用messenger的android绑定服务指南示例,并运行以下代码片段。 我稍微修改了这个片段,使它有一个hi和bye按钮,服务应该显示toast hi或bye,具体取决于UI上按下的按钮 但在运行示例时,不会显示toast。尽管收到的消息日志打印在UI上 原因何在?。活动开始时的祝酒词也不显示 public class MessengerService extends Service { public final static int MSG_SAY_HELLO = 1;
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();
}
});
}
}
您忘记调用
show()
方法了
试试这个代码剪
Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show()
您忘记调用
show()
方法了
试试这个代码剪
Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show()
据我所知,你没有调用show方法。尝试按以下方式更改代码:
Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show();
据我所知,你没有调用show方法。尝试按以下方式更改代码:
Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show();
您忘记调用Toast链末尾的“.show()”方法:
Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show();
您忘记调用Toast链末尾的“.show()”方法:
Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show();
典型的新手错误!我几乎总是这么做。谢谢你指出。典型的新手错误!我几乎总是这么做。谢谢你指出。典型的新手错误!我几乎总是这么做。谢谢你指出。典型的新手错误!我几乎总是这么做。谢谢你指出。