如何从扩展另一个类成活动的Java类返回值 [英] How to return values from a java class that extends another class into an Activity

查看:206
本文介绍了如何从扩展另一个类成活动的Java类返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于如何从扩展另一个类成活动的Java类返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆