Android Studio中的setText&天文台问题

时间:2018-02-06 20:00:31

标签: java android toast settext chronometer

我创建了一个用于下载我已经完成的模块文件的应用程序!该应用程序运行良好!我正在尝试在文本视图中显示从FTP服务器下载的活动文件。但我无法显示超过3条消息。我创建计时器,当应用程序下载文件时,计时器STOP在10秒......并在2m30sec下载所有文件后重新启动....为什么??? 在IF条件下启动班次中的所有消息都很好......但是其他消息都不起作用! 我需要帮助...我发布了我的部分代码和我需要做的事情。 感谢大家回复!

吉拉: 我们已经为钻井人员创建了一个应用程序。此应用程序连接到模块以下载文件。一天结束后,司钻将执行停止班次。我们想在他开始和停止他的班次时向司钻显示一条消息。 StartShift 1017 - 1032的行动 StopShift 1035 - 1063的行动

我创建了一个textview来向用户显示应用程序的活动。 textView的名称:textViewDuration。

1 - 我们需要在此textview中显示文件下载。 在DashBoardActivity.java的第794和846行 喜欢:“正在下载:nameOfFile”

2 - 我们需要在此textview中显示上传文件名称。 在FtpMultiUploadTask.java的第195,265和329行 喜欢:“上传:nameOfFile”

我不知道为什么,我只能显示3条消息。 在DashBoardActivity的第1026行... textViewDuration.setText(“发送'T'文件”); 在DashBoardActivity的第1134行... textViewDuration.setText(“下载所有文件......请等待”); 并在DashBoardActivity的第1208行重置为空... textViewDuration.setText(“”);

一旦应用程序显示这3条消息,它就不会显示其他消息。 我在活动下载,FtpMultiUpload或停止班次上执行的所有setText都不起作用。 DashBoardActivity的第1035行,第1047行,第1049行,第1052行,第1054行,第1057行。 当下载和上传文件时,我需要帮助来更改TextView的文本。

private void startShift() {


    if (isToggleButtonChecked == (false)) {

        writeLog(DashBoardActivity.this, "Version Unidrill: 0.3.13");
        writeLog(DashBoardActivity.this, "Conn. Unibridge: " + UserPreferenceHelper.PREF_HOTSPOT);
        writeLog(DashBoardActivity.this, "SSID: " + UserPreferenceHelper.getInstance(DashBoardActivity.this).getString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SSID_NAME));
        writeLog(DashBoardActivity.this, "START SHIFT");
        //Toast.makeText(this , "Start Shift",Toast.LENGTH_LONG);
        mChronometer.setBase(SystemClock.elapsedRealtime());
        mChronometer.start();
        textViewDuration.setText("Sending 'T' file");
        indicateur.setVisibility(toggleButtonStart.VISIBLE);
        isToggleButtonChecked = true;
        toggleButtonStart.setText("STOP SHIFT");
        UserPreferenceHelper.getInstance(this).saveString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SHIFT_START, "true");

        uniDrillApplication.runTaskModeDEMO();

    } else {

/*
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textViewDuration.setText("Downloading all files ... Please Wait");
            }
        });
*/
        ((TextView) findViewById(R.id.textViewDuration)).setText("STOP SHIFT");
        //textViewDuration.setText("Downloading all files ... Please Wait");
        UserPreferenceHelper.getInstance(this).saveString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SHIFT_START, "false");
        UserPreferenceHelper.getInstance(this).saveString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_IS_STOP_SHIFT, "0");
        uniDrillApplication.is_downloaded = "0";

        downloadFile();
        this.textViewDuration.setText("");
        SystemClock.sleep(5000);
        uniDrillApplication.stopTask();
        writeLog(DashBoardActivity.this, "STOP SHIFT");
        writeLog(DashBoardActivity.this, "CREATE PDF REPORT");
        textViewDuration.setText("Create Report PDF");
        createPDF();
        textViewDuration.setText("");
        SystemClock.sleep(5000);
        writeLog(DashBoardActivity.this, "PREPARING TO SEND MAIL");
        textViewDuration.setText("Waiting wifi with internet");
        sendMail();
        textViewDuration.setText("Send Mail");
        SystemClock.sleep(10000);
        showProgress();
        textViewDuration.setText("Sending file to FTP UNIDRAULIK");
        sendFileToServer();
        textViewDuration.setText("");
        indicateur.setVisibility(toggleButtonStart.INVISIBLE);
        isToggleButtonChecked = false;
        toggleButtonStart.setText("START SHIFT");
        mChronometer.stop();
        mChronometer.setBase(SystemClock.elapsedRealtime());



    }

}



 private Boolean download(Context ctx, String server, int portNumber, String user, String password){
    //this.textViewDuration.setText("Rendu dans le download");
    ((TextView) findViewById(R.id.textViewDuration)).setText("Debut du download!!!");
    showProgress();
    //toggleButtonStart.setText("Preparing to download file");
    boolean success =false;
    //dismissProgressDialog();
    final Pattern PATTERN = Pattern.compile("(.*?)(?:\\((\\d+)\\))?(\\.[^.]*)?");
    final Context _ctx = ctx;
    String _server = server;
    int _portNumber = portNumber;
    String _user = user;
    String _password = password;
    final String UNIBRIDGE_FILE_PATH = "/mnt/myDrive";
    String sdcardDestinationPath;
    FTPClient ftpClient = new FTPClient();
    List<String> _listFile = new ArrayList<>();
    sdcardDestinationPath = Environment.getExternalStorageDirectory()+"/"+ this.getApplicationContext().getPackageName();
    int TENSECONDS  = 10*1000;
    //showToast("DOWNLOADING ALL FILES! PLEASE WAIT ...");
    //Toast.makeText(this, "Downloading ALL files! Please Wait!", Toast.LENGTH_LONG).show();
    //if (toastTest != null){
    //    toastTest.cancel();
    //}
    //toastTest = Toast.makeText(this, "Preparing to download ALL files! Please Wait...", Toast.LENGTH_SHORT);
    //toastTest.show();
    try {
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.setConnectTimeout(8000);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        //showCustomPopup(GenericDialog.GenericDialogType.NONE, "Connexion to FTP UNIBRIDGE", null, null, null);
        ftpClient.connect(_server, _portNumber);
        writeLog(this,"Connexion to ftp unibridge ...");

        boolean isConnected = ftpClient.login(_user, _password);
        //prg.setProgress(15);
        if (isConnected) {

            writeLog(this,"Connexion to ftp unibridge = OK");
            //showCustomPopup(GenericDialog.GenericDialogType.WARNING, "Connexion to FTP UNIBRIDGE = OK", null, null, null);
 /*
                //ALERTE AVEC TIMER A 2 SECONDES
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Connected to Unibridge FTP");
            builder.setMessage("OK");
            builder.setCancelable(true);
            final AlertDialog dlg = builder.create();
            dlg.show();
            final Timer t = new Timer();
            t.schedule(new TimerTask() {
                public void run() {
                    dlg.dismiss(); // when the task active then close the dialog
                    t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                }
            }, 2000);
*/
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.printWorkingDirectory();
            ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
            //showServerReply(_ctx);
            FTPFile[] files = ftpClient.listFiles();
            //prg.setProgress(20);
            for (FTPFile file : files) {

                final String fileName = file.getName();

                //textViewDuration.setText("Checking file: " + fileName);


                //Context baseCtx = getBaseContext();
                //Context appCtx = getApplicationContext();
/*
                DashBoardActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), fileName, Toast.LENGTH_LONG).show();
                    }
                });
                */
                //Toast.makeText(getBaseContext(), fileName, Toast.LENGTH_LONG).show();
                /*
                DashBoardActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), fileName, Toast.LENGTH_LONG).show();
                    }
                });
                **/
                /*
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DashBoardActivity.this, fileName, Toast.LENGTH_LONG).show();
                    }
                });
                */
                //Toast.makeText(this, file.getName(), Toast.LENGTH_LONG).show();
                ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
                boolean verifFile = false;
                String name = file.getName();
                if (file.isDirectory()) {
                    //TODO :  Vérifier pourquoi le fichier 700101 existe. Idéalement faire conditions contraire (if not en java)
                    if(!name.matches("\\d+(?:\\.\\d+)?") || (name.matches("700101"))) {
                        //return false;
                    }else{
                        writeLog(this, "current working directory : "+ftpClient.printWorkingDirectory());
                        writeLog(this, "change working directory to : " + name);
                        ftpClient.changeWorkingDirectory(name);
                        writeLog(this, "new working directory : "+ftpClient.printWorkingDirectory());

                        FTPFile[] unibridgeFiles = ftpClient.listFiles();

                        int i = 0;
                        for (FTPFile unibridgefile : unibridgeFiles) {
                            FTPFile[] fileDirectory = ftpClient.listFiles();
                            if(fileDirectory.length == 2){
                                writeLog(this, "Delete directory 1: "+ name);
                                writeLog(this, "___________________");
                                ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
                                boolean checkDelete = ftpClient.removeDirectory(name);
                            }
                            if (! unibridgefile.isDirectory()) {
                                //download
                                OutputStream outputStream = null;
                                try {
                                    writeLog(this, "LocalFilePath : " + sdcardDestinationPath);
                                    writeLog(this, "File : " + unibridgefile.getName());

                                    if(!".".equals(unibridgefile.getName()) && !"..".equals(unibridgefile.getName())){

                                        if (unibridgefile.getName().contains(".csv")){
                                            //String synchroFileName = "T"+ DateTools.getHours(this);
                                            String synchroFileName = DateTools.getDateToString(new Date(),FORMAT_DATE_SYNCHRO);
                                            System.out.println(synchroFileName);
                                            String hh = "-" + synchroFileName.substring(6,8);
                                            String date = synchroFileName.substring(0,6);
                                            String checkShift = UserPreferenceHelper.getInstance(this).getString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SHIFT_START);

                                            if(checkShift.equals("true")){
                                                if (unibridgefile.getName().contains("D") && unibridgefile.getName().contains(date) && unibridgefile.getName().contains(hh)){

                                                }else if(unibridgefile.getName().contains("E") && unibridgefile.getName().contains(date)){

                                                }else if(unibridgefile.getName().contains("R") && unibridgefile.getName().contains(date)){

                                                }else{
                                                    //Toast.makeText(getApplicationContext(), "Downloading: 00000000" , Toast.LENGTH_LONG).show();
                                                    //SystemClock.sleep(2000);
                                                    File _file = new File(sdcardDestinationPath, unibridgefile.getName());
                                                    FileOutputStream destFileStream = new FileOutputStream(_file);
                                                    //if (toastTest != null){
                                                    //    toastTest.cancel();
                                                    //}
                                                    //toastTest = Toast.makeText(getBaseContext(), "Downloading: " + unibridgefile.getName(), Toast.LENGTH_SHORT);
                                                    //toastTest.show();

                                                    outputStream = new BufferedOutputStream(destFileStream);
                                                    /*
HERE WE NEED MESSAGE TO SHOW EACH FILE IS DOWNLOADING
 */
                                                    ftpClient.retrieveFile(unibridgefile.getName(), outputStream);
                                                    final String nameFile = unibridgefile.getName();




                                                    //Toast.makeText(getApplicationContext(),"Your message",Toast.LENGTH_LONG).show();


                                                    //toast.setText("Downloading: " + nameFile);
                                                    //toast.setDuration(Toast.LENGTH_LONG);
                                                    //toast.show();
/*
                                                    Runnable changeText = 
new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(DashBoardActivity.this, "Downloading: " + nameFile, Toast.LENGTH_SHORT).show();
                                                        }
                                                    };

runOnUiThread(changeText);
                                                    */
                                                    /*
                                                    final Handler handler= new Handler();
                                                    final Runnable r = new Runnable() {
                                                        public void run() {
                                                            Toast.makeText(DashBoardActivity.this, "Downloading: " + nameFile, Toast.LENGTH_SHORT).show();

 //handler.postDelayed(this, 1000);
                                                        }
                                                    };
                                                    handler.postDelayed(r, 5);
*/
                                                    /*
                                                    Handler handler = new Handler(Looper.getMainLooper());
                                                    handler.post(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(getApplicationContext(),"Downloading: " + nameFile,Toast.LENGTH_SHORT).show();
                                                        }
                                                    });
*/
                                                    boolean deletefile = ftpClient.deleteFile(unibridgefile.getName());


                                                }
                                            }else{
                                                File _file = new File(sdcardDestinationPath, unibridgefile.getName());
                                                FileOutputStream destFileStream = new FileOutputStream(_file);
                                                outputStream = new BufferedOutputStream(destFileStream);

                                                /*
HERE WE NEED MESSAGE TO SHOW EACH FILE IS DOWNLOADING
 */
                                                ftpClient.retrieveFile(unibridgefile.getName(), outputStream);





                                                //Handler handler = new Handler(Looper.getMainLooper());

                                                //handler.post(new Runnable() {

                                                //   @Override
                                                //    public void run() {
                                                //        Toast.makeText(DashBoardActivity.this.getApplicationContext(),"Downloading: tesssst",Toast.LENGTH_SHORT).show();
                                                //    }
                                                //});
                                                boolean deletefile = ftpClient.deleteFile(unibridgefile.getName());
                                                textViewDuration.setText("");
                                                /*
                                            _listFile.add(unibridgefile.getName());

                                            writeLog(_ctx,"Download file :" +  unibridgefile.getName() + " in progress.");

                                            String fileName = "";
                                            File _file = new File(sdcardDestinationPath, unibridgefile.getName());


                                            Matcher m = PATTERN.matcher(unibridgefile.getName());
                                            if (m.matches()) {
                                                String prefix = m.group(1);
                                                String last = m.group(2);
                                                String suffix = m.group(3);
                                                if (suffix == null) suffix = "";

                                                int count = 0;

                                                while(_file.exists()) {
                                                    count++;
                                                    fileName = prefix + "(" + count + ")" + suffix;
                                                    _file = new File(sdcardDestinationPath, fileName);
                                                    verifFile = true;
                                                }
                                            }

                                            FileOutputStream destFileStream = new FileOutputStream(_file);
                                            outputStream = new BufferedOutputStream(destFileStream);

                                            success = ftpClient.retrieveFile(unibridgefile.getName(), outputStream);
                                            if (verifFile){
                                                String line = "";
                                                BufferedReader br = new BufferedReader(new FileReader(sdcardDestinationPath + "/" + fileName));
                                                br.readLine();
                                                FileWriter fw = new FileWriter(sdcardDestinationPath + "/" + unibridgefile.getName(), true);
                                                while((line = br.readLine()) != null){
                                                    fw.append(line + "\n");
                                                }
                                                fw.flush();
                                                fw.close();
                                                br.close();
                                                _file.delete();


                                            }

                                            if(success){
                                                //writeLog(_ctx, unibridgefile.getName()+" downloaded to "+ sdcardDestinationPath);
                                                String deleteFilePath = ftpClient.printWorkingDirectory() + "/" + unibridgefile.getName();
                                                //boolean test = ftpClient.deleteFile(deleteFilePath);
                                                //System.out.println(test);

                                                boolean deletefile = ftpClient.deleteFile(unibridgefile.getName());
                                                FTPFile[] fileDirectory2 = ftpClient.listFiles();
                                                if(fileDirectory2.length == 2){
                                                    ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
                                                    boolean checkDelete = ftpClient.removeDirectory(name);
                                                    writeLog(_ctx, "Delete directory: "+ name);
                                                    writeLog(_ctx, "____________________ ");
                                                }
                                            }else{
                                                writeLog(_ctx, unibridgefile.getName()+" not downloaded to "+ sdcardDestinationPath);
                                            }
                                            */
                                            }
                                        }
                                    }

                                } finally {
                                    if (outputStream != null) {
                                        outputStream.close();

                                    }
                                }

                            }
                        }
                        writeLog(this, "current working directory : "+ftpClient.printWorkingDirectory());
                        writeLog(this, "change working directory to parent ");
                        ftpClient.changeToParentDirectory();
                        writeLog(this, "new working directory : "+ftpClient.printWorkingDirectory());
                       /* if(success){
                            int test = ftpClient.dele(ftpClient.printWorkingDirectory());
                            System.out.println(test);
                            boolean delete = ftpClient.deleteFile(ftpClient.printWorkingDirectory());
                            System.out.println(delete);
                        }*/
                    }
                }
            }

            if (ftpClient.isConnected()){
                ftpClient.disconnect();
            }
            dismissProgressDialog();
            return success;
        } else {
            dismissProgressDialog();
            writeLog(this, "Connexion to ftp unibridge = KO");
            return false;
        }
    } catch (Exception e) {
        Log.d("FTP", e.toString());
        writeLog(this, "Exception ="+ e.toString());
        // return false;
        success =false;
    }finally {

        //if the IO is timed out or force disconnected, exceptions may be thrown when trying to logout/disconnect
        try
        {

            ftpClient.setSoTimeout(TENSECONDS);
            ftpClient.logout();

        }
        catch(Exception innerException)
        {

            success = false;

        }
        finally
        {

            try {
                ftpClient.disconnect();
            }catch (IOException ex){
                ex.printStackTrace();
            }

            success = false;
        }

    }
    textViewDuration.setText("");
    return success;

}

this is when i open app

When i press Start Shift... the textView change ... but the chrono stay to 0!

The chrono start at 0:05, and the text change... but i wan't show to user the name of file ... not a please wait :)

When all file is downloded, the text view dissapear ... and i press stop shift, but the text view never show again and the chrono stop

After all task are done, the chrono is reset to 0, and ready to start other shift!

图片#1:这是我打开应用程序的时候 图2:当我按开始转换... textView更改...但计时保持为0! 图3:计时器从0:05开始,文本改变了......但我不向用户显示文件名...不是请等待:) 图4:当所有文件下载时,文本视图消失...我按停止班次,但文本视图从不再显示,计时停止 图#5:完成所有任务后,计时器重置为0,准备开始其他任务!

0 个答案:

没有答案