2013-02-07 2 views
0

나는 서로 통신 할 수 있어야하는 서비스와 활동을 모두 만들었습니다. 활동은 서비스에 메시지를 보내야하며 서비스는 결과로 응답해야합니다.Android 서비스가 수신하지 않음 활동으로부터의 메시지

문제는 메시지가 서비스에 표시되지 않는 것입니다. 다음 코드에서 로그에 출력됩니다.

02-07 18:26:37.057: I/Task Timer(8850): Service bound 
02-07 18:26:37.097: V/Task Timer(8850): Service connected: ComponentInfo{com.gawdl3y.android.tasktimer/com.gawdl3y.android.tasktimer.TaskService} 
02-07 18:26:37.097: I/Task Timer(8850): Activity sent message: { what=1 when=-1d11h33m57s160ms } 
02-07 18:26:37.167: I/ActivityManager(482): Displayed com.gawdl3y.android.tasktimer/.TaskListActivity: +166ms 

응용 프로그램의 다른 로그 항목은 기록되지 않습니다. 대신 바인더)를 messenger.getBinder를 (반환 시도 할 수

TaskService.java

public class TaskService extends Service { 
    public static String TAG = null; 

    private final IBinder binder = new ServiceBinder(); 
    private Messenger messenger = new Messenger(new IncomingHandler()), activityMessenger; 

    @Override 
    public void onCreate() { 
     Log.i(getString(R.string.app_name), "Service started"); 
     TAG = getString(R.string.service_label); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return binder; 
    } 

    @Override 
    public void onDestroy() { 
     Log.i(TAG, "Service destroyed"); 
    } 

    public class ServiceBinder extends Binder { 
     TaskService getService() { 
      return TaskService.this; 
     } 
    } 

    private final class IncomingHandler extends Handler { 
     @Override 
     public void handleMessage(Message msg) { 
      activityMessenger = msg.replyTo; 
      Log.i(TAG, "Service received message: " + msg); 

      switch(msg.what) { 
      case TaskListActivity.MSG_GET_TASKS: 
       // Create the response message and send it 
       Message response = Message.obtain(null, 1, tasks); 
       try { 
        activityMessenger.send(response); 
        Log.i(TAG, "Service sent message: " + response); 
       } catch(android.os.RemoteException e) { 
        Log.i(TAG, "Service failed to send message: " + response + " (" + e.getLocalizedMessage() + " caused by " + e.getCause() + ")"); 
       } 

       // Return the message to the global pool 
       response.recycle(); 

       break; 
      default: 
       super.handleMessage(msg); 
      } 
     } 
    } 
} 

TaskListActivity.java 당신의 onBind 방법에

public class TaskListActivity extends SherlockActivity { 

    public static final int MSG_GET_TASKS = 1; 

    private TaskService service; 
    private Messenger messenger = new Messenger(new IncomingHandler()), serviceMessenger; 
    private boolean connected = false; 

    private ServiceConnection serviceConnection = new ServiceConnection() { 
     public void onServiceConnected(ComponentName name, IBinder service) { 
      Log.v(TAG, "Service connected: " + name); 

      // Set the service messenger and connected status 
      TaskListActivity.this.serviceMessenger = new Messenger(service); 
      TaskListActivity.this.connected = true; 

      // Create a new message to send to retrieve the tasks 
      Message msg = Message.obtain(null, MSG_GET_TASKS); 
      msg.replyTo = messenger; 

      // Send the message 
      try { 
       serviceMessenger.send(msg); 
       Log.i(TAG, "Activity sent message: " + msg); 
      } catch(android.os.RemoteException e) { 
       Log.i(TAG, "Activity failed to send message: " + msg + " (" + e.getLocalizedMessage() + " caused by " + e.getCause() + ")"); 
      } 

      // Return the message to the global pool 
      msg.recycle(); 
     } 

     public void onServiceDisconnected(ComponentName name) { 
      Log.v(TAG, "Service disconnected: " + name); 

      // Reset the service messenger and connection status 
      TaskListActivity.this.serviceMessenger = null; 
      TaskListActivity.this.connected = false; 
     } 
    }; 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     // Show the loading indicator 
     //setSupportProgressBarIndeterminateVisibility(true); 

     // Start and bind the service 
     Intent intent = new Intent(this, TaskService.class); 
     startService(intent); 
     if(bindService(intent, serviceConnection, Context.BIND_ABOVE_CLIENT | Context.BIND_ADJUST_WITH_ACTIVITY)) { 
      Log.i(TAG, "Service bound"); 
     } else { 
      Log.i(TAG, "Service not bound"); 
      Toast.makeText(this, "Task Service couldn't be bound", Toast.LENGTH_SHORT).show(); 
      finish(); 
     } 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 

     // Unbind service 
     unbindService(serviceConnection); 
     Log.i(TAG, "Service unbound"); 
    } 

    private final class IncomingHandler extends Handler { 
     @Override 
     public void handleMessage(Message msg) { 
      switch(msg.what) { 
      case MSG_GET_TASKS: 
       Log.i(TAG, "Activity received message: " + msg); 
       onTasksLoaded((ArrayList<Task>) msg.obj); 
       break; 
      default: 
       super.handleMessage(msg); 
      } 
     } 
    } 
} 
+0

서비스와 통신하기 위해 처리기가 필요한 이유를 이해할 수 없습니다. 서비스에 바인드하고 서비스가 활동과 동일한 프로세스에있는 경우 서비스의 메소드를 직접 호출 할 수 있다고 생각합니다. – CChi

+0

@ CChi 처음에는 시도했지만, 그랬을 때 즉시 사용할 수 없었습니다. 그것은 null로 시작했다. 그래도 onServiceConnected 안에서 시도하지 않았습니다. 해보고 싶다면 TaskService 객체를 참조 할 수 있어야하지만 다른 프로세스 (: TaskService)에 있기 때문에 가능하지 않다고 생각합니다. 실제로 TaskService (TaskListActivity.this.service = ((TaskService.ServiceBinder) 서비스) .getService())에 서비스를 할당하려고하면 "android.os.BinderProxy를 com.gawdl3y.android로 형변환 할 수 없습니다. .tasktimer.TaskService $ ServiceBinder "로 변경하십시오. – Gawdl3y

+0

onBind 메서드에서 바인더 대신 messenger.getBinder()를 반환 해 볼 수 있습니까? – CChi

답변

2

: 여기 내 관련 코드입니까?

+0

메시징 문제가 해결되었습니다. 감사합니다. ;) – Gawdl3y

관련 문제