0

我已经创建了需要能够相互通信的服务和活动。Activity 应该向 Service 发送 Message,Service 应该回复一个结果。

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

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

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

任务服务.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);
            }
        }
    }
}

任务列表活动.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);
            }
        }
    }
}
4

1 回答 1

2

在您的 onBind 方法中,您可以尝试返回 messenger.getBinder() 而不是活页夹吗?

于 2013-02-08T18:57:35.233 回答