在活动之间传递类引用

时间:2016-01-10 00:00:04

标签: java android android-activity

我有一个活动Test.java,我在其中声明了我的自定义类PPMDecoder。

类PPMDecoder使用侦听器接口生成活动的中断(以便更新UI)。

PPMdecoder类的updateChannelUpperLimits事件是我定期在活动SecondActivity.java上放置一些我需要的数据的地方。 PPMdecoder类还生成我在Test.java中使用的其他事件,现在我将其移除以便于您使用。

问题: 我想使用来自SecondActivity.java中updateChannelUpperLimits事件的数据,该事件是在用户请求时调用的(来自选项菜单)。 注意:我无法在SecondActivity中创建PPMdecoder类的第二个实例,因为我需要使用PPMDecoder类的单个实例(现在我在Test.java中声明,因为我使用它从另一个事件中获取数据我从IAsyncFetchListener.java中删除的PPMdecoder类,以方便您使用)

哪种方法最好?

1)我可以将Test.java Activity上启动的类PPMDecoder的引用传递给SecondClass吗?

2)或者我可以从Test.java活动更新SecondActivity的TextViews吗?

3)或者有一种最简单的方法吗?

这些是实际文件:

Test.java的过滤版本

 public class Test extends Activity {

PPMDecoder ppmdecoder;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);


    ppmdecoder=new PPMDecoder(this);
    ppmdecoder.setListener(new IAsyncFetchListener() {

        public void updateChannelUpperLimits(ArrayList<Float> channelUpperLimits ) {
            //Here i receive data to use to update a TextView in a new Activity
        }



    });

    int result=ppmdecoder.startDecoding();

   }

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
     super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    Intent intent;
    switch (item.getItemId()) {
        case R.id.action_decoder_calibration:


            //here i open another activity to show data in a different way          
                intent = new Intent(this, SecondActivity.class);
                startActivityForResult(intent, 2);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

 }      

接口文件IAsyncFetchListener.java

 import java.util.ArrayList;
 import java.util.EventListener;


 public interface IAsyncFetchListener extends EventListener {

void updateChannelUpperLimits(ArrayList<Float> channelUpperLimits );
 //here i declare also other events that i use    

 }

PPMdecoder.java类的过滤版本

 public class PPMDecoder {
 private ArrayList<Float> channelUpperLimits; //array with upper limits of the channels in milliseconds
IAsyncFetchListener fetchListener = null;

 public void setListener(IAsyncFetchListener listener) {
        this.fetchListener = listener;
    }

public PPMDecoder(Context context)
{
}

  public class RecordAudio extends AsyncTask<Void, Void, Throwable> {

    @Override
    protected Throwable doInBackground(Void... arg0) {

            while (started) {
                    // Here is where i update the channelUpperLimits ArrayList on each loop.
                    //each 100ms publish the results on the UI
                    elapsedTimeSinceLastPublish=System.currentTimeMillis()-lastPublishMilliseconds;
                    if (elapsedTimeSinceLastPublish>100){
                        publishProgress();
                        lastPublishMilliseconds=System.currentTimeMillis();

                     }

             }

    }

    @Override
    protected void onProgressUpdate(Void... arg0) {
            //generate interupt in the father thread
            if (fetchListener != null) fetchListener.updateChannelUpperLimits(channelUpperLimits);
        }


} 

}

1 个答案:

答案 0 :(得分:1)

如果你想避免你的类有多个实例我建议用单例转换你的PPMDecoder类,在这种情况下你可以在你需要的每个类中调用getInstance,而不需要复制。 所以你的班级类似于这样的

public class PPMDecoder {
    private static final String TAG = PPMDecoder.class.getSimpleName();
    private ArrayList<Float> channelUpperLimits; //array with upper limits of the channels in milliseconds
    IAsyncFetchListener fetchListener = null;

    public void setListener(IAsyncFetchListener listener) {
        this.fetchListener = listener;
    }

    private static PPMDecoder instance = null;

    protected PPMDecoder(){}

    public static PPMDecoder getInstance(){
        if(instance == null)
            instance = new PPMDecoder();
        return instance;
    }

    ....
    ....
}

在您需要的任何活动中,只需致电:

PPMDecoder.getInstance().setListener(new IAsyncFetchListener() {

    public void updateChannelUpperLimits(ArrayList<Float> channelUpperLimits ) {
        //Here i receive data to use to update a TextView in a new Activity
    }



});

希望得到这个帮助。