Android服务未接收来自活动的消息

时间:2013-02-07 23:28:57

标签: java android android-service

我创建了一个需要能够相互通信的服务和活动。活动应向服务发送消息,服务应回复结果。

问题是Message似乎没有进入服务。这是代码输出到日志:

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

记录了应用程序中的其他任何内容。 这是我的相关代码:

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

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

1 个答案:

答案 0 :(得分:2)

在你的onBind方法中,你可以尝试返回messenger.getBinder()而不是绑定器吗?