0

Android 開発は初めてで、サービスからアクティビティへの通信について混乱しています。ソケットをリッスンする必要があり、パケットを受信したら、アクティビティで何かを更新します。サービスでソケットをリッスンする予定で、何かを受信したときにアクティビティに伝えたいと考えています。

今のところ、Service が 5 秒ごとに Activity にメッセージを送信するようにしたいだけです。アクティビティがメッセージを受信すると、それを TextView に出力します。

私は(Bound Services)を見てきましたが、これはサービスのメソッドを呼び出すアクティビティの1つの方法にすぎないようです。放送は必要ですか?プロセス間通信じゃないの?

誰かが私を正しい方向に向けることができますか? ありがとう。

4

1 に答える 1

0

コールバックを使用します。

ServiceInterface.aidl:

package com.test.service;
import com.test.service.ServiceCallback;
interface ServiceInterface {

    /**
     * Register callback for information from service.
     */
    void registerCallback(ServiceCallback cb);

    /**
     * Remove a previously registered callback interface.
     */
    void unregisterCallback(ServiceCallback cb);
}

ServiceCallback.aidl

package com.test.service;

/**
 * Callback interface used by IRemoteService to send
 * synchronous notifications back to its clients.  Note that this is a
 * one-way interface so the server does not block waiting for the client.
 */
oneway interface ServiceCallback {
    /**
     * Your callback function
     */
    void onCallback(int data);
}

より多くのコードで説明しているMalachi の Androidサイトへのリンクを見つけました。

于 2013-04-18T13:02:05.813 に答える