且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

没有收到来自活动消息的Andr​​oid服务

更新时间:2022-12-18 22:29:02

在您的onBind方法,你可以尝试返回,而不是粘结剂messenger.getBinder()?

I have created both a Service and an Activity that need to be able to communicate with each other. The Activity should send a Message to the Service, and the Service should reply with a result.

The problem is that the Message doesn't seem to be making it to the Service. This is the output to the log from the code:

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

Nothing else from the app is logged. Here's my relevant code:

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

In your onBind method, can you try returning the messenger.getBinder() instead of the binder?