0

Let's say that I've the following main activity:

public class MwConsoleActivity extends Activity {

    private classChild child = null;    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        child = new classChild();
    }
}

Then consider the implementation of the class "classChild":

public class MwBondingAgent extends SapereAgent {

    MwBondingAgent(){}

    public void AddEventListener(childAddedEvent event) {

       //Send the data of event back to the main activity

    }
}

I've tried to use IntentServices but was not able to receive the values back to the main activity. What would be the approach I've to take?

Cheers Ali

4

3 に答える 3

2

ブロードキャストをリッスンするには、intentFilter を使用できます。これをアクティビティに追加します。

 IntentFilter intentFilter = new IntentFilter(
                "com.unique.name");
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //extract our message from intent
                String msg_for_me = intent.getStringExtra("some_msg");
             }
        };
        //registering our receiver
        this.registerReceiver(mReceiver, intentFilter);

クラスで、これをアクティビティに通知する部分に追加します。

Intent i = new Intent("com.unique.name").putExtra("some_msg", "I have been updated!");
this.sendBroadcast(i);
于 2012-10-31T14:41:47.100 に答える
1

あなたの質問は非常に不明確ですが、あなたが望んでいるのは、アクティビティへのコールバックを実装することだと思います。これは、インターフェースを使用して行うことができます。

public class MwConsoleActivity extends Activity implements MwBondingAgent{

    private classChild child = null;    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        child = new classChild();
    }

    @Override
    public void gotEventData(EventData myEventData) {

        //to whatever you want with myEventData
    }
}

そして、あなたの他のクラスで。

public class MwBondingAgent extends SapereAgent {

    private MwBondingAgentCallback activityCallback;

    MwBondingAgent(Activity callback){

         activityCallback = callback;
    }

    public void AddEventListener(childAddedEvent event) {

        //Send the data of event back to the main activity
        EventData myEventData = //got some event data;
        //Send it back to activity
        activityCallback.gotEventData(myEventData);
    }

    public interface MwBondingAgentCallback {

        public void gotEventData(EventData myEventData);
    }
}
于 2012-10-31T14:39:46.970 に答える
1

オブザーバー / リスナー パターンを使用する必要があります。

http://www.vogella.com/articles/DesignPatternObserver/article.html

これは、MVC アーキテクチャ パターンを使用する場合に最もよく使用される設計パターンの 1 つです。

于 2012-10-31T14:38:08.137 に答える