无法停止执行AsyncTask

时间:2016-07-14 14:06:42

标签: java android android-asynctask

我不能阻止我的AsyncTask无论我做了什么; cancel(),将所有工作放入while循环,然后打破等等。

我的任务是从Mysql服务器获取/发送数据。我知道这是最糟糕的方式,但这是我在java中执行了一些步骤之后的第一次尝试。

所以我想执行并完成所有工作,并在显示布局之前停在onCreate()。(setContentView()之前)。因为,如果我点击一个使用数据库中的数据填充微调器的按钮可能尚未下载。

我不想检查我用来填充微调器的arraylist,如果它是" null"不正常工作的原因。

因此,如果我可以这样做,我可以向用户显示关于连接失败的一些警告。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

connect2 cnc=new connect2();
    cnc.execute();



    if (cnc.getStatus()==AsyncTask.Status.FINISHED)
    {
        setContentView(R.layout.window2);
    }


}

  private class connect2 extends AsyncTask<Void , Void, Void> {


    @Override
    protected Void doInBackground(Void... params) {

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(a, b, c);
            PreparedStatement prs = conn.prepareStatement("select * from film , kategori,yonetmen where film.kategori_id=kategori.id and film.yonetmen_id=yonetmen.id");
            ResultSet rs = prs.executeQuery();
            PreparedStatement prs2 = conn.prepareStatement("select * from kategori ");
            ResultSet rs2 = prs2.executeQuery();
            PreparedStatement prs3 = conn.prepareStatement("select * from yonetmen ");
            ResultSet rs3 = prs3.executeQuery();

            while (rs.next()) {
                Kategori k = new Kategori(rs.getInt("kategori.id"), rs.getString("kategori.ad"));
                Yonetmen y = new Yonetmen(rs.getInt("yonetmen.id"), rs.getString("yonetmen.ad"));
                Film f = new Film(rs.getInt("id"), rs.getString("ad"), rs.getInt("sene"), k, y);
                ab.add(f);

            }
            rs.close();
            size = ab.size();
            while (rs2.next()) {
                Kategori k = new Kategori(rs2.getInt("kategori.id"), rs2.getString("kategori.ad"));
                ac.add(k);

            }
            rs2.close();
            while (rs3.next()) {
                Yonetmen k = new Yonetmen(rs3.getInt("yonetmen.id"), rs3.getString("yonetmen.ad"));
                ad.add(k);

            }
            rs3.close();

            switch (sw) {

                case 1:

                    StringBuilder cmd = new StringBuilder();
                    cmd.append("insert into film (ad,sene,yonetmen_id,kategori_id) values ('");
                    cmd.append(fa + "',");
                    cmd.append(ya + ",");
                    cmd.append(idy + ",");
                    cmd.append(idk + ")");
                    sw = 0;


                    try {
                        PreparedStatement sql = conn.prepareStatement(cmd.toString());
                        rslt = sql.executeUpdate();

                        conn.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;

                case 2:


                    try {
                        PreparedStatement sql = conn.prepareStatement("delete from film where id=" + idy);
                        rslt2 = sql.executeUpdate();
                        sw = 0;

                        conn.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    break;
                default:
                    break;

            }
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;


    } @Override
    protected void onPostExecute(Void aVoid) {

        list=ab;
        listk=ac;
        listy=ad;


        if (rslt==1) {


            AlertDialog.Builder builder1 = new AlertDialog.Builder( MainActivity.this);
            builder1.setMessage("Film Başarıyla Kaydedildi");
            builder1.setCancelable(true);

            builder1.setPositiveButton(
                    "Tamam",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });



            AlertDialog alert11 = builder1.create();
            alert11.show();
            rslt=0;

        }

        if (rslt2==1) {


            AlertDialog.Builder builder1 = new AlertDialog.Builder( MainActivity.this);
            builder1.setMessage("Silindi");
            builder1.setCancelable(true);

            builder1.setPositiveButton(
                    "Tamam",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });



            AlertDialog alert11 = builder1.create();
            alert11.show();
            rslt2=0;

        }

        super.onPostExecute(aVoid);
    }

2 个答案:

答案 0 :(得分:0)

像这样实施。尝试打破部分代码,检查是否取消

doInBackground(){
   if(!isCancelled()){
     // few line of codes
   }else{
     return null;
   }
   if(!isCancelled()){
     // another few line of codes
   }else{
     return null;
   }
   if(!isCancelled()){
     // another few line of codes
   }else{
     return null;
   }
}

答案 1 :(得分:0)

AsyncTask.cancel()不会杀死线程而不考虑后果。它只是将AsyncTask设置为“已取消”状态。 AsyncTask的开发人员可以在doInBackground()中遵守取消状态。