2014-09-18 2 views
0

messenger를 사용하고 다음 코드 단편을 실행하는 바운드 서비스에 대한 안드로이드 가이드의 예제를 따르고 있습니다. 스 니펫을 약간 수정하여 hi 및 bye 버튼이 있고 서비스에서 UI에서 눌려진 버튼에 따라 토스트 하이 또는 바이를 표시해야합니다.메신저를 사용하여 바운드 서비스 예제에 토스트가 표시되지 않음

하지만 예제 토스트 실행시에는 표시되지 않습니다. 메시지 수신 로그는 UI에 인쇄되지만

이유는 무엇입니까? Acticity의 onStart에서의 축배 또한 표시되지 않습니다.

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); 
       case MSG_SAY_BYE: 
        Toast.makeText(getApplicationContext(), "bye!", Toast.LENGTH_SHORT); 
       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(); 
      } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.my, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

답변

2

이 코드 싹둑를 시도 show() 메소드를 호출 잊고있다.

Toast.makeText(getApplicationContext(), "hello!", Toast.LENGTH_SHORT).show() 
+0

고전적 신인상! 나는 거의 항상 그렇게한다. 지적 해 주셔서 고마워요. –

2

지금까지 내가보기에는 show 메소드를 호출하지 않습니다. 다음과 같이 코드를 변경해보십시오 :

Toast.makeText(this, "not bound yet", Toast.LENGTH_SHORT).show(); 
+0

고전 신참 실수! 나는 거의 항상 그렇게한다. 지적 해 주셔서 고마워요. –

관련 문제