如何从扩展另一个类成活动的Java类返回值返回值、Java

由网友(╰︶ ̄ 莫等闲)分享简介:让我们说,我有以下主要活动:Let's say that I've the following main activity:public class MwConsoleActivity extends Activity {private classChild child = null; public void...

让我们说,我有以下主要活动:

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

然后考虑的类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

    }
}

我试图用IntentServices但无法接收值回主要活动。什么是我所采取的方法呢?

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?

干杯 阿里

推荐答案

您可以使用和IntentFilter的侦听广播。 这种添加到活动中:

You can use and intentFilter to listen for broadcasts. Add this to the activity:

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

在你的类将它添加到您要通知该活动的一部分:

In your class add this to the part you want to notify the activity:

Intent i = new Intent("com.unique.name").putExtra("some_msg", "I have been updated!");
this.sendBroadcast(i);
阅读全文

相关推荐

最新文章