如何防止由于Android中的异步任务而导致UI冻结?

时间:2019-06-15 19:00:42

标签: java android

我正在创建一个应用,该应用通过异步任务通过Internet从FTP服务器下载文件。 这是我的代码的一部分:

        async=new netAsyn(admin, password, gate, this,file_explorer.this);

        try {
            files=async.execute(url).get();
        } catch (ExecutionException | InterruptedException e) {
            e.printStackTrace();
        }

        async.cancel(true);

        if(files==null) {


            beer.setImageResource(R.drawable.pooky);
            beer.setVisibility(View.VISIBLE);
            beer.setAnimation(appear);
            beer.startAnimation(appear);
            funnytext.setVisibility(View.VISIBLE);
            trigger=1;
            bot.setAnimation(down);
            bot.startAnimation(down);
            funnytext.setText("Pooky is sad.\n" + "He could'nt connect to your server right now");
            Toast.makeText(this, "Can't connect", Toast.LENGTH_LONG).show();
        }
        else {

            trigger=0;
            beer.setVisibility(View.INVISIBLE);
            funnytext.setVisibility(View.INVISIBLE);

            adapter = new grid_adapter(files, getApplicationContext());
            gridView.setAdapter(adapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    filename = files[position].getName();
                    url = url + "/" + filename;

                    async = new netAsyn(admin, password, gate, con,file_explorer.this);
                    if (files[position].isDirectory()) {
                        //try {
                        try {
                            files=async.execute(url).get();

                        if(files.length==0)
                            {
                                trigger=1;
                                bot.setAnimation(down);
                                bot.startAnimation(down);
                                beer.setImageResource(R.drawable.beer_mug);
                                beer.setVisibility(View.VISIBLE);
                                beer.setAnimation(appear);
                                beer.startAnimation(appear);
                                funnytext.setVisibility(View.VISIBLE);
                                funnytext.setText("Empty.Just like my beer mug.");
                            }
                            else{
                                trigger=0;
                                beer.setVisibility(View.INVISIBLE);
                                funnytext.setVisibility(View.INVISIBLE);
                            }

                      } catch (ExecutionException | InterruptedException e) {
                            e.printStackTrace();
                        }
                        async.cancel(true);
                        adapter.updatelist(files);
                        adapter.notifyDataSetChanged();
                        gridView.setAdapter(adapter);
                    } else {
                        beer.setVisibility(View.INVISIBLE);
                        funnytext.setVisibility(View.INVISIBLE);
                        downloader = new downup(admin, password,con);
                        downloader.execute(url);
                        Toast.makeText(getApplicationContext(), "Downloading" + url, Toast.LENGTH_SHORT).show();
                        downloader.cancel(true);
                        url = popper(url);
                    }
                }
            });

netAsync类-doInBackground:

public class netAsyn extends AsyncTask<String,String,FTPFile[]> {
    public interface results
    {
        void getFiles(FTPFile[] file);
    }
    results delegate;
    FTPClient client = null;
    FTPFile[] dir = null;
    Activity activity;
    String choice;
    String gate;
    Boolean connectstat;
    String password;
    String admin;
    Context mContext;
    DBHelper dbsql;
    public netAsyn(String admn,String pass,String gateway,Context context,Activity act)
    {
        this.mContext=context;
        this.admin=admn;
        activity=act;
        this.password=pass;
        this.gate=gateway;

    }
    @Override
    protected FTPFile[] doInBackground(String... strings) {
        SharedPreferences preferences = mContext.getSharedPreferences("Themes", 0);
        String gate=preferences.getString("CurrentKey","null");
        dbsql=new DBHelper(mContext);
        String g=dbsql.getGateway(gate);
        FTPFile f=new FTPFile();

        if (client == null) {
            client = new FTPClient();
            client.setConnectTimeout(5000);
            try {
                client.connect(g);

            } catch (IOException e) {

                e.printStackTrace();
                Log.d("new", "Connection failed...");
                return null;
            }

        }

        boolean login = false;
        try {
            login = client.login(admin, password);
            Log.d("new", "Connection established...");


            if (isCancelled()) { Log.d("new", "over");
                Log.d("new", "over");
                return null;
            }
            client.changeWorkingDirectory(strings[0]);
            dir = client.listFiles();

            for (FTPFile file : dir) {
                Log.d("new", file.getSize() + "     " + file.getLink());
            }



            if(strings[0].equals("__BACK__"))
            {
                client.changeToParentDirectory();
                dir = client.listFiles();

                for (FTPFile file : dir) {
                    Log.d("new1", file.getName() + "     " + file.getLink());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return dir;
    }

每当我尝试使用netAsync任务通过文件下载的文件名更新gridview时,UI都会冻结。请建议我 编辑: 我发现问题是由asynctask的get方法引起的。但是如何不使用该方法返回fileArray?

0 个答案:

没有答案