PublishProgress不工作

时间:2016-10-14 09:51:30

标签: android android-asynctask textview

我做了几个小时的研究和阅读这些事情,但我仍然无法弄清楚我的代码的问题: 我有一个带有按钮的视图和两个TextView。 单击该按钮,将调用一个过程,该过程将下载文件,对其进行分析并将计算结果打印到其中一个TextView。如果无限计算开始,则另一个只接受状态。 理论如此之多。 实际上几乎所有东西都可以工作,除了publishProgress应该将计算输出打印到TextView的部分。 (计算再次开始,但TextView中不显示任何文本。)

我希望有人可以帮助我解决这个问题。

这是我的代码:

package com.protonmail.fabian.schneider.aim;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class AIM_start extends AppCompatActivity {
    static String downURL = "http://services.swpc.noaa.gov/text/goes-magnetometer-primary.txt";
    static String errPatt = "-1.00e+05";
    public TextView status;
    private Button startButton;
    private Button stopButton;
    public TextView output;
    start_AIM calc = new start_AIM();
    private boolean func = false;

    public static String strengthArr[] = new String[4];

    static {
        strengthArr[0] = "90,110,90,110";
        strengthArr[1] = "50,89,111,150";
        strengthArr[2] = "0,49,151,200";
        strengthArr[3] = "-50,-1,201,-250";
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aim_start);
        status = (TextView) findViewById(R.id.statusView);
        output = (TextView) findViewById(R.id.lbl_output);
        startButton = (Button) findViewById(R.id.start_aim);
        startButton.setOnClickListener(new View.OnClickListener (){
            public void onClick(View v) {
                if (!func) {
                    func = true;
                    startButton.setText("Stop AIM");
                    status.setText("AIM-Started");
                    calc = new start_AIM();
                    calc.execute();
                } else {
                    func = false;
                    startButton.setText("Start AIM");
                    calc.cancel(false);
                    status.setText("AIM-Stopped");
                }
            }
        });
    }



    class start_AIM extends AsyncTask<String, String, String> {

        public String doInBackground(String... params){
            while (!this.isCancelled()) {

                System.out.println("before publishProgress Update");
                //publishProgress("Calculation Started");

                dataImport dI;
                try {
                    dI = new dataImport(downURL);
                    String calcData;
                    calcData = dI.download();

                    System.out.println(calcData);
                    preAnalysis pA;
                    pA = new preAnalysis(calcData);
                    boolean dateOk;
                    dateOk = pA.preanalyse();
                    if (dateOk) {
                        System.out.println("Data Date is okay");
                        //call last line downloader
                        dI = new dataImport(downURL);
                        String lastLine;
                        lastLine = dI.downloadLast();
                        if (!lastLine.contains(errPatt)) {
                            //data is ok
                            System.out.println("Current data is okay");
                            System.out.println("Current data: " + lastLine);
                            System.out.println("Starting analysis");

                            analyse an = new analyse(lastLine);
                            int strength;
                            strength = an.analyseData();
                            if (strength != -1) {
                                System.out.println("Strength: " + strength);
                                //outputText(Integer.toString(strength));
                                publishProgress(Integer.toString(strength));
                                //return Integer.toString(strength);


                            } else {
                                System.out.println("Strength not found");
                                //send signal for out of scope to output
                                publishProgress("-1");
                                //return "-1";
                            }
                        } else {
                            System.out.println("Current data is not okay");
                            publishProgress("Current data is not okay");
                            //send signal for satellite down to output
                            publishProgress("-1");
                            //return "-1";
                        }

                    } else {
                        System.out.println("Data Date not okay");
                        publishProgress("Data Date not okay");
                        //send signal for satellite down to output
                        publishProgress("-1");
                        //return "-1";
                    }


                    //catch statements
                } catch (MalformedURLException e) {
                    System.out.println("MalformedUrlException for dI allocation");
                } catch (IOException e) {
                    System.out.println("IOException in dI alloc");

                    //check inet conn/send satellite down to output
                    return "Connection expired";
                }
                //return "ERROR";
            }
            return null;
        }
        @Override protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
        }

        protected void outputText(String result) {
            output.setText(result);
        }

        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            Log.d("MyApp", "finished");
            outputText(result);
        }


        protected void onPreExecute(String text) {
            status.setText(text);
        }

        protected void onProgressUpdate(String text) {
            super.onProgressUpdate(text);
            Log.d("MyApp", "onProgressUpdate called");
            status.setText(text);

        }


        final class dataImport {
            private String downData = "";
            private URL url = null;
            private String lastLine = "";

            dataImport(String url) throws MalformedURLException {
                URL allocUrl = new URL(url);
                this.url = allocUrl;
            }

            String download() throws IOException {
                URLConnection con = url.openConnection();
                InputStream ins = con.getInputStream();

                BufferedReader br = new BufferedReader(new InputStreamReader(ins));

                String line;

                while ((line = br.readLine()) != null) {
                    downData += line;
                    lastLine = line;
                }
                return downData;
            }

            String downloadLast() throws IOException {
                this.download();
                return lastLine;
            }
        }

        class preAnalysis{
            private String data;
            private String dataDate;
            private String sDate;
            private Calendar dateToCheck = Calendar.getInstance();
            private Calendar timeToCheck = Calendar.getInstance();

            preAnalysis(String data){
                this.data = data;
            }

            boolean preanalyse(){
                dataDate = this.getDataDate(data);
                sDate = this.getDataTime(dataDate);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMM dd HHmm");


                //TODO make date convert right
                try {
                    dateToCheck.setTime(formatter.parse(dataDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                formatter = new SimpleDateFormat("HHmm");
                try {
                    timeToCheck.setTime(formatter.parse(sDate));
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                //convert from UT
                Calendar currentDate = Calendar.getInstance();
                Calendar currentTime = Calendar.getInstance();
                currentTime.add(Calendar.HOUR, -2);
                currentTime.add(Calendar.MINUTE, -5);
                //currentTime.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH,Calendar.HOUR -2, Calendar.MINUTE -5);

                System.out.println("dateToCheck: " + dateToCheck + "; current Date: " + currentDate);
                System.out.println("timeToCheck: " + timeToCheck + "; current Time: " + currentTime);
                System.out.println("timeToCheck Time:" + timeToCheck.HOUR + ":" + timeToCheck.MINUTE);
                System.out.println("time checked: " + currentTime.HOUR + ":" + currentTime.MINUTE);
                if(dateToCheck.YEAR == currentDate.YEAR && dateToCheck.MONTH == currentDate.MONTH &&
                        dateToCheck.DAY_OF_MONTH == currentDate.DAY_OF_MONTH &&
                        timeToCheck.HOUR == currentTime.HOUR && timeToCheck.MINUTE == currentTime.MINUTE){      //&& !timeToCheck.before(currentTime.getTime())
                    return true;
                } else {
                    return false;
                }
            }
            private String getDataDate(final String data){
                return data.substring(35,51);
            }

            private String getDataTime(final String dataDate){
                return dataDate.substring(12);
            }
        }


        final class analyse{
            private String data;
            private String[] splittedData = new String[4];
            private Double[] splittedStrength = new Double[4];
            analyse(String data){
                this.data = data;
            }

            int analyseData(){
                data = this.getCalcData();
                this.splitCalcData();
                String temp = splittedData[3];
                double tempSplit = Double.parseDouble(temp);
                int counter = 0;
                for (String i : strengthArr){
                    this.splitStrength(i);
                    if ((tempSplit >= splittedStrength[0] &&
                            tempSplit <= splittedStrength[1]) ||
                            (tempSplit >= splittedStrength[2] &&
                                    tempSplit <= splittedStrength[3])){
                        return counter;
                    }
                    counter += 1;
                }

                return -1;
            }

            private void splitStrength(String strength){
                String[] temp;
                temp = strength.split(",");
                for(int b = 0; b < temp.length; b++){
                    splittedStrength[b] = Double.parseDouble(temp[b]);
                }
            }


            private String getCalcData(){
                return data.substring(37);
            }

            private void splitCalcData(){
                splittedData[0] = data.substring(0,8);
                splittedData[1] = data.substring(13,21);
                splittedData[2] = data.substring(25,33);
                splittedData[3] = data.substring(35);
            }
        }


        abstract class dataOut{
            void out(){

            }
        }

        final class lblOut extends AppCompatActivity {
            void outputToLbl(String value){
                final TextView textViewToChange = (TextView) findViewById(R.id.textView);
                textViewToChange.setText(value);
            }
        }

        final class bluetoothOut extends dataOut{
            void bluetoothOut(){

            }
        }

        final class audioOut extends dataOut {
            void audioOut() {

            }
        }






    } //fin start aim


} //fin main

1 个答案:

答案 0 :(得分:0)

您的onProgressUpdate

似乎错过了方法start_AIM extends AsyncTask
protected void onProgressUpdate(Integer... progress) {
         doSomething(progress[0]);
}

文档在这里:

https://developer.android.com/reference/android/os/AsyncTask.html