-2
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 添加到其中,但我不知道该怎么做。请帮我!

4

1 回答 1

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);
} 
}
于 2012-10-17T13:10:11.803 回答