线程需要单击应用程序的按钮两次

时间:2015-08-18 18:54:17

标签: android multithreading

我有以下代码。当有人点击我的应用程序上的提交按钮时,会触发submitClick()。在过去,提交点击将被点击一次,并且将执行所有的submitFile()。通过向checkContent()添加线程添加,现在必须单击submitClick两次才能完成所有submitFile()。是否有任何方法可以使submitFile()完成(使用checkContent()),以便用户只需按下我的应用提交按钮?

提前谢谢。

public void submitClick(View v) throws Exception{
    if (!submitted) { submitFile(); }
}

public void submitFile() {
    checkContent();
    if (valid) {
        Picasso.with(this).load(imageAddress).into(imagePreview);
        saveImage();
        //sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); //refreshes system to show saved file
        submitted = true;
        submit_but.setText("Encrypt");
    }
}

private void checkContent() {
    media = null;
    if(URLUtil.isValidUrl(imageAddress)) {
        Thread thread = new Thread() {
            boolean img = false;
            boolean youtube = false;
            public void run() {
                URLConnection connection = null;
                try {
                    connection = new URL(imageAddress).openConnection();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentType = connection.getHeaderField("Content-Type");
                img = contentType.startsWith("image/");
                if(img)
                    media = "image";
                if (!img) {
                    // Check host of url if youtube exists
                    Uri uri = Uri.parse(imageAddress);
                    if ("www.youtube.com".equals(uri.getHost())) {
                        media = "youtube";
                        youtube = true;
                    }
                }
                valid = img || youtube;
            }
        };
        thread.start();
    }
}

1 个答案:

答案 0 :(得分:0)

问题是Thread会在致电if (valid)之后完成,其余部分会在submitFile()之后完成。

简单的解决方法是将整个submitFile()包含在一个Thread中,而不仅仅是其中的一部分。如果逻辑彼此联系在一起,那么他们在一起就会变得更好。

更具安全性的解决方法是使用AsyncTask,如下所示:

public void submitClick(View v) throws Exception {
    if (!submitted) { submitFile(); }
}

public void submitFile() {
    if(URLUtil.isValidUrl(imageAddress)) {  
        new AsyncTask<Void, Void, Boolean>() {
            protected Long doInBackground(Void... voids) {
                boolean img = false;
                boolean youtube = false;
                URLConnection connection = null;
                try {
                    connection = new URL(imageAddress).openConnection();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentType = connection.getHeaderField("Content-Type");
                img = contentType.startsWith("image/");
                if(img)
                    media = "image";
                if (!img) {
                    // Check host of url if youtube exists
                    Uri uri = Uri.parse(imageAddress);
                    if ("www.youtube.com".equals(uri.getHost())) {
                        media = "youtube";
                        youtube = true;
                    }
                }
                return img || youtube;
            }

            protected void onPostExecute(Boolean valid) {
                if (valid) {
                    Picasso.with(this).load(imageAddress).into(imagePreview);
                    saveImage();
                    //sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); //refreshes system to show saved file
                    submitted = true;
                    submit_but.setText("Encrypt");
                }
            }
        }.execute();
    }
}