-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();
        }

I get ANR error while executing this code when the data is fetching takes time so i would like to add AsyncTask to it but i dont know to to do. please help me!

abbas.aniefa
  • 2,855
  • 21
  • 30
user1310156
  • 87
  • 2
  • 7
  • 3
    You should make a research on the topic and come up with more specific question. Stackoverflow is not for asking someone to write code for you. – Egor Oct 17 '12 at 12:54
  • I agree but i would like to know how to implement it i have done some research on it but could not get it correctly. – user1310156 Oct 17 '12 at 12:59
  • http://developer.android.com/reference/android/os/AsyncTask.html. Go through the link and try implementing it on your own. If you still find difficulties you can post a new question on that topic – Raghunandan Oct 17 '12 at 13:02
  • -1 for not doing research on the topic – Rajeev N B Oct 17 '12 at 13:05
  • @user1310156, Then you should provide the details of your research and specify what exactly you can't understand. – Egor Oct 17 '12 at 13:08
  • -1 for not doing research on the topic – Raghunandan Oct 17 '12 at 13:20

1 Answers1

1

follow like this:

@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);
} 
}
SubbaReddy PolamReddy
  • 2,083
  • 2
  • 17
  • 23