使用doInBackground()获取的多个值作为onPostExecute()中的条目

时间:2014-06-08 09:44:03

标签: android android-asynctask androidplot

我正在尝试使用Asynctask来简化Android上几个图表的绘图。 在doInBackground()中,我计算了3个不同的数组(n,l和g2),这些数组将是onPostExecute()中要绘制的值。

问题是,一旦我获得了doInBackGround()中的值,我真的不知道如何将它们用作onPostExecute()中的条目,任何人都能帮我一把吗?

private class Operaciones2 extends AsyncTask<Void, Integer, Void> {
    int progress;   
    double [] st =new double [arr.length];
    int j=0;


    protected void onPostExecute(Void unused ){



        series1 = new SimpleXYSeries(
                    Arrays.asList(n),          // SimpleXYSeries takes a List so turn our array into a List
                    SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                    "Series1");   

             series2 = new SimpleXYSeries(
                    Arrays.asList(l),          // SimpleXYSeries takes a List so turn our array into a List
                    SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                    "Series1"); 

             series3 = new SimpleXYSeries(
                    Arrays.asList(g2),          // SimpleXYSeries takes a List so turn our array into a List
                    SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
                    "Series1");
          //Plot1
              //RANGE (Y)
              //plot.setRangeBoundaries(min, max, BoundaryMode.FIXED);
              plot.setRangeStepValue(10);
              widget.getRangeOriginLinePaint().setColor(Color.BLACK);//Color linea de origen eje Y
              widget.getRangeOriginLabelPaint().setColor(Color.BLACK);// Color etiqueta origen eje Y
              widget.getRangeLabelPaint().setColor(Color.BLACK);//Color de las letras/numeros eje Y
              plot.setRangeLabel("Amplitud"); //Etiqueta eje Y
              plot.getRangeLabelWidget().getLabelPaint().setColor(Color.BLACK);//Color Etiqueta eje y

              // DOMAIN (X)
              plot.setDomainBoundaries(0,n.length, BoundaryMode.FIXED);
              plot.setDomainStepValue(10);
              widget.getDomainOriginLinePaint().setColor(Color.BLACK);//Color linea de origen eje X
              widget.getDomainOriginLabelPaint().setColor(Color.BLACK);// Color etiqueta origen eje X
              widget.getDomainLabelPaint().setColor(Color.BLACK);//Color de las letras/numeros eje X
              plot.setDomainLabel("Tiempo");//Etiqueta eje X
              plot.getDomainLabelWidget().getLabelPaint().setColor(Color.BLACK);//Color Etiqueta eje X

              //Configs
              plot.setTitle("Representación forma de onda");
              plot.getTitleWidget().getLabelPaint().setColor(Color.BLACK);
              plot.getTitleWidget().getLabelPaint().setTextSize(20);
              widget.setMarginRight(25);//para que no corte los numeros del eje X
              widget.setMarginTop(10);//para que no corte los numeros del eje Y
              plot.getBorderPaint().setColor(Color.TRANSPARENT);
              plot.getBackgroundPaint().setColor(Color.TRANSPARENT);
              widget.getBackgroundPaint().setColor(Color.TRANSPARENT);
              widget.getGridBackgroundPaint().setColor(Color.TRANSPARENT);//Color de la grafica en si (no el layout)
              //plot.addSeries(series1, series1Format);

              plot.setDomainValueFormat(new DecimalFormat() {
                  @Override
                  public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {

                     double z=(Main.GRABA/1000.0)/n.length;
                     double sec = Math.round((value * z)*10.0)/10.0;
                     return new StringBuffer (sec + " Sec");
                  }

                  @Override
                  public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) {
                      throw new UnsupportedOperationException("Not yet implemented.");
                  }

                  @Override
                  public Number parse(String string, ParsePosition position) {
                      throw new UnsupportedOperationException("Not yet implemented.");
                  }
              });

              //Plot2
              //RANGE (Y)
              plot2.setRangeStepValue(12);
              widget2.getRangeOriginLinePaint().setColor(Color.BLACK);//Color linea de origen eje Y
              widget2.getRangeOriginLabelPaint().setColor(Color.BLACK);// Color etiqueta origen eje Y
              widget2.getRangeLabelPaint().setColor(Color.BLACK);//Color de las letras/numeros eje Y
              plot2.setRangeLabel("Amplitud"); //Etiqueta eje Y
              plot2.getRangeLabelWidget().getLabelPaint().setColor(Color.BLACK);//Color Etiqueta eje y
              widget2.setMarginLeft(25);
              plot2.setRangeBoundaries(min2, max2, BoundaryMode.FIXED);

              // DOMAIN (X)
              plot2.setDomainBoundaries(0,n.length, BoundaryMode.FIXED);
              plot2.setDomainStepValue(20);
              widget2.getDomainOriginLinePaint().setColor(Color.BLACK);//Color linea de origen eje X
              widget2.getDomainOriginLabelPaint().setColor(Color.BLACK);// Color etiqueta origen eje X
              widget2.getDomainLabelPaint().setColor(Color.BLACK);//Color de las letras/numeros eje X
              plot2.setDomainLabel("Muestras");//Etiqueta eje X
              plot2.getDomainLabelWidget().getLabelPaint().setColor(Color.BLACK);//Color Etiqueta eje X

              //Configs
              plot2.setTitle("Representación forma de onda");
              plot2.getTitleWidget().getLabelPaint().setColor(Color.BLACK);
              plot2.getTitleWidget().getLabelPaint().setTextSize(20);
              widget2.setMarginRight(25);//para que no corte los numeros del eje X
              widget2.setMarginTop(10);//para que no corte los numeros del eje Y
              plot2.getBorderPaint().setColor(Color.TRANSPARENT);
              plot2.getBackgroundPaint().setColor(Color.TRANSPARENT);
              widget2.getBackgroundPaint().setColor(Color.TRANSPARENT);
              widget2.getGridBackgroundPaint().setColor(Color.TRANSPARENT);//Color de la grafica en si (no el layout)
              //plot2.addSeries(series2, series1Format);
              plot2.setDomainValueFormat(new DecimalFormat() {
                    @Override
                    public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {

                       double z=(Main.GRABA/1000.0)/n.length;
                       double sec = Math.round((value * z)*100.0)/100.0;
                       return new StringBuffer (sec + " Sec");
                    }

                    @Override
                    public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) {
                        throw new UnsupportedOperationException("Not yet implemented.");
                    }

                    @Override
                    public Number parse(String string, ParsePosition position) {
                        throw new UnsupportedOperationException("Not yet implemented.");
                    }
                });
            //Plot3
              //RANGE (Y)
              plot3.setRangeStepValue(12);
              widget3.getRangeOriginLinePaint().setColor(Color.BLACK);//Color linea de origen eje Y
              widget3.getRangeOriginLabelPaint().setColor(Color.BLACK);// Color etiqueta origen eje Y
              widget3.getRangeLabelPaint().setColor(Color.BLACK);//Color de las letras/numeros eje Y
              plot3.setRangeLabel("Amplitud"); //Etiqueta eje Y
              plot3.getRangeLabelWidget().getLabelPaint().setColor(Color.BLACK);//Color Etiqueta eje y
              widget3.setMarginLeft(25);
              plot3.setRangeBoundaries(minFFT, maxFFT, BoundaryMode.FIXED);

              // DOMAIN (X)
              plot3.setDomainBoundaries(0,Main.sizeFFT, BoundaryMode.FIXED);
              plot3.setDomainStepValue(n.length/1000);
              widget3.getDomainOriginLinePaint().setColor(Color.BLACK);//Color linea de origen eje X
              widget3.getDomainOriginLabelPaint().setColor(Color.BLACK);// Color etiqueta origen eje X
              widget3.getDomainLabelPaint().setColor(Color.BLACK);//Color de las letras/numeros eje X
              plot3.setDomainLabel("Muestras");//Etiqueta eje X
              plot3.getDomainLabelWidget().getLabelPaint().setColor(Color.BLACK);//Color Etiqueta eje X

              //Configs
              plot3.setTitle("Representación forma de onda");
              plot3.getTitleWidget().getLabelPaint().setColor(Color.BLACK);
              plot3.getTitleWidget().getLabelPaint().setTextSize(20);
              widget3.setMarginRight(25);//para que no corte los numeros del eje X
              widget3.setMarginTop(10);//para que no corte los numeros del eje Y
              plot3.getBorderPaint().setColor(Color.TRANSPARENT);
              plot3.getBackgroundPaint().setColor(Color.TRANSPARENT);
              widget3.getBackgroundPaint().setColor(Color.TRANSPARENT);
              widget3.getGridBackgroundPaint().setColor(Color.TRANSPARENT);//Color de la grafica en si (no el layout)
              //plot3.addSeries(series3, series1Format);
              plot3.setDomainValueFormat(new NumberFormat() {
                @Override
                public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {

                    double f=Math.round(Main.SAMPLES/Main.sizeFFT);
                    int frec = (int) (value * f) ;
                    return new StringBuffer(frec + " Hz");
                }

                @Override
                public StringBuffer format(long value, StringBuffer buffer, FieldPosition field) {
                    throw new UnsupportedOperationException("Not yet implemented.");
                }

                @Override
                public Number parse(String string, ParsePosition position) {
                    throw new UnsupportedOperationException("Not yet implemented.");
                }
            });
        plot.addSeries(series1, series1Format);
        plot2.addSeries(series2, series1Format);
        plot3.addSeries(series3, series1Format);
        plot.redraw();
        plot2.redraw();
        plot3.redraw();
        Toast.makeText(getApplicationContext(), "Todo cargado", Toast.LENGTH_LONG).show();
    }

    protected Void doInBackground(Void... unused){


            for (int i = 44; i < s; i+=2) {
                // convert byte pair to int
                double audioSample = (double) (array[i+1] << 8 | array[i] & 0xff)/ 32767.0;

                arr[j]=audioSample;  //double
                n[j] = (Number)arr[j];  //Number

                st[j]=10*Math.log10(Math.abs((audioSample/Math.pow(10, -12)))); //double
                l[j]=(Number)(10*Math.log10(Math.abs((audioSample/Math.pow(10, -12)))));  //Number

                if(audioSample == 0.0 ){
                    if(j!=0){
                    l[j]=l[j-1];
                    st[j]=st[j-1];
                    }else{
                        l[j]=0.0;
                        st[j]=0.0;
                    }
                  }
                //progress++;
                publishProgress((i/s)*1000);            
                j=j+1;}
                min=Operaciones.minimum(arr);
                max=Operaciones.maximum(arr);
                min2=Operaciones.minimum(st);
                max2=Operaciones.maximum(st);
                /*****************************/
                arreaFFT();

            return null;

    }
 }

2 个答案:

答案 0 :(得分:0)

您只能使用以下代码返回一个数组:

public class Operaciones2 extends AsyncTask<Integer, Void, Integer[]> {

@Override
protected int doInBackground(int... arg0) {
    int example[] = new int[4];

    //put what you want
    example[0] = 1;
    example[1] = 2;
    example[2] = 3;
    example[3] = 5574;
    return example;}

@Override
protected void onPostExecute(int result[]) {
    Toast.makeText(Activity.this, "Result: " + result[0] + ", " + result[1], Toast.LENGTH_SHORT).show();}}

但您可以在Operaciones2类中创建公共数组,并按如下方式对其进行编辑:

public class Operaciones2 extends AsyncTask<Integer, Void, Void> {

int a[] = new int[4];
String b[] = new String[4];

@Override
protected void doInBackground(int... arg0) {

    //put what you want
    a[0] = 1;
    a[1] = 2;
    b[2] = "abc";
    b[3] = "defghijk";}

@Override
protected void onPostExecute(Void result) {
    Toast.makeText(Activity.this, "Result: " + b[0] + ", " + a[2], Toast.LENGTH_SHORT).show();}}

答案 1 :(得分:0)

更好的解决方案是创建一个包含您要返回的所有数据的自定义类,例如

private static class CalcResults
{
    public Number[] n;
    public Number[] l;
    public Number[] g2;
}

private class Operaciones2 extends AsyncTask<Void, Integer, CalcResults>
{
    @Override
    protected CalcResults doInBackground(Void... unused)
    {
        CalcResults r = new CalcResults();
        ... same code, using r.n instead of n, r.l instead of l, and so on ...
        return r;
    }

    @Override
    protected void onPostExecute(CalcResults results)
    {
        ...
    }
}

如果AsyncTask的输入也很复杂,你也可以有一个类。它比使用“全局”变量要清晰得多。