如何在我的代码上实现AsyncTask?

时间:2012-10-17 12:52:20

标签: android android-anr-dialog

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //connMgr = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);

        //if ((connMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
            //  || (connMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)) {



        if(new CheckInternetAvaliable(MobileReagentsActivity.this).isInternetAvaliable) {

            morePrefs = this.getSharedPreferences("morePrefs", MODE_WORLD_READABLE);

            final int welcomeScreenDisplay = 5000;   

            db = new VendorRatingDatabaseHandler(this);

            isDataInserted = morePrefs.getBoolean("flagValue", false);

            Thread welcomeThread = new Thread() {

                int wait = 0;

                @Override
                public void run() {
                    try {
                        super.run();
                        while (wait < welcomeScreenDisplay) {
                            sleep(100);
                            wait += 100;
                        }
                    } catch (Exception e) {
                        System.out.println("EXc=" + e);
                    } finally {
                        Intent intent = new Intent(MobileActivity.this, HomeScreen.class);

                        intent.putExtra("message", message);
                        intent.putExtra("topMolNames", topMolNames);
                        intent.putExtra("topFomls", topFomls);
                        intent.putExtra("lastConnect", lastConnect);
                        intent.putExtra("dateDiff", dateDiff);
                        intent.putExtra("moldrawPrefs", moldrawPrefs);

                        startActivity(intent);
                        Log.i("Test", "Starting Home Screen!! ");
                        MobileActivity.this.finish();
                    }
                }
            };
            welcomeThread.start();

            try {

                SAXParserFactory saxPF = SAXParserFactory.newInstance();
                SAXParser saxP = saxPF.newSAXParser();
                XMLReader xmlR = saxP.getXMLReader();

                URL url = new URL(ConstantsClass.MORE_STARTUP_MESSAGE_URL + "?uid="+new AndroidUdIdClass(MobileActivity.this).androidId+"&device=android"); // URL of the XML



                XMLHandler myXMLHandler = new XMLHandler();
                xmlR.setContentHandler(myXMLHandler);
                xmlR.parse(new InputSource(url.openStream()));

            } catch (Exception e) {
                System.out.println(e);
            }

             getsetXML = XMLHandler.data;


             message = getsetXML.getMessage();
             vendorListFull = getsetXML.getVendorListFull();
             vendorListUser = getsetXML.getVendorListUser();
             topMolNames = getsetXML.getTopMolNames();
             topFomls = getsetXML.getTopFomls();
             lastConnect = getsetXML.getLastConnect();
             dateDiff = getsetXML.getDateDiff();
             moldrawPrefs = getsetXML.getMoldrawPrefs();

             prefsEditor = morePrefs.edit();
             prefsEditor.putString("UserVendorList", vendorListUser);
             prefsEditor.putString("VendorListFull", vendorListFull);
             prefsEditor.commit();

             //---------------------------------------------------------

             String[] str_arr_vendor_list = vendorListFull.split(",");

             for(int i=0; i<str_arr_vendor_list.length; i++) {

                 VendorRatingData vendorRatingData = new VendorRatingData();

                 vendorRatingData.setVendorName(str_arr_vendor_list[i]);
                 vendorRatingData.setVendorRating("0");

                 VendorRatingData vendor_rating = new VendorRatingData();


                 if(!isDataInserted) {

                     db.addVendorRating(vendorRatingData);      

                 }else {

                     vendor_rating = db.getRating(str_arr_vendor_list[i]);

                    // Toast.makeText(getApplicationContext(), "vendor_rating::" + i, Toast.LENGTH_LONG).show();
                     if(!(vendor_rating.getVendorName().equalsIgnoreCase(str_arr_vendor_list[i]))) {

                         db.addVendorRating(vendorRatingData);
                     }
                 }


             }

             db.close();

             prefsEditor = morePrefs.edit();
             prefsEditor.putBoolean("flagValue", true);
             prefsEditor.commit();
        }

我在获取数据时执行此代码时出现ANR错误需要时间,所以我想向它添加AsyncTask但我不知道要做什么。请帮帮我!

1 个答案:

答案 0 :(得分:1)

像这样:

@Override
protected void onNewIntent(Intent intent) {
    //this is very important, otherwise you would get a null Scheme in the onResume later on.
    setIntent(intent);
}

public class FetchAlbums extends AsyncTask<Void, Void, ArrayList<Photoset>> {

//declarations what u required
@Override
protected void onPreExecute() {          
super.onPreExecute();
}

@Override
protected ArrayList<PhotoSet> doInBackground(Void... params) {

    //do ur work here completly that will runs as background
}

@Override
protected void onPostExecute(ArrayList<PhotoSet> result) {       
super.onPostExecute(result);
} 
}