0

相互に通信できる必要がある Service と Activity の両方を作成しました。アクティビティはサービスにメッセージを送信し、サービスは結果を返す必要があります。

問題は、メッセージがサービスに到達していないように見えることです。これは、コードからのログへの出力です。

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

1 に答える 1

2

onBindメソッドで、バインダーの代わりにmessenger.getBinder()を返してみることができますか?

于 2013-02-08T18:57:35.233 に答える