-1

I'm currently writing a program to get data from a server as a xml file and store it in a simple database and display the locations on a google map

I developed the app for android 2.2 and I tested it with android ICS in emulator. It worked as I expected but when I try it on my android device which runs android 4.04 the application stuck

If you have any idea why such happening please help me Thank you

  • 1
    post your real device's logcat trace with the error – Adrián Rodríguez Dec 20 '12 at 11:43
  • 12-20 17:27:39.439: W/ActivityManager(291): Launch timeout has expired, giving up wake lock! 12-20 17:27:39.469: W/ActivityManager(29 1): Activity idle timeout for ActivityRecord{2beb4118 jayanga.entersys.map/jayanga.entersys.xml.Update} I call update activity from my main activity to get xml data when the application goes to the update activity the screen does not display anything at all – jayangaVliyanage Dec 20 '12 at 11:59
  • Are you doing network operations and accessing database on the UI thread? – Egor Dec 20 '12 at 12:22
  • I use separate activity to access network. when a button clicked the it goes to another activity and connect to the network and update the database then return back to the first activity.. – jayangaVliyanage Dec 21 '12 at 03:37

3 Answers3

0

@arcastro, @Stephan Celis, @Egor and @Mohan thank you very much for your help. All your ideas helped me a lot. I add a separate thread to connect to the network and download data @Egor your question made me realize what I did wrong

Thank you all

For developers who stuck in the same problem which i did hear is how i did it

place below "updateHandler" code in your class

 private Handler updateHandler = new Handler()
   {
       @Override
       public void handleMessage(Message msg)
       {
           if (msg.what == -999)
           {
            Log.d("recieve data","from http://jayanga.esplprojects.com/xx/data.xml to update");
                     DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                     String xml = XML.getXML("http://jayanga.esplprojects.com/xx/data.xml");

                     Document doc = XML.XMLfromString(xml);
                    int numResults = XML.numResults(doc);

                     if((numResults <= 0)){
                            Toast.makeText(getApplicationContext(), "SORRY No data were found", Toast.LENGTH_LONG).show();  
                            finish();
                     }//if((numResults <= 0))

                     NodeList nodes = doc.getElementsByTagName("result");

                     for (int i = 0; i < nodes.getLength(); i++) {                          

                            Element e = (Element)nodes.item(i);
                        //  String s = "...."+XML.getValue(e, "name");
                            // Inserting Contacts
                            Log.d("Insert: ", "Inserting ..");
                            db.addContact(new Details(XML.getValue(e, "name"),Double.parseDouble( XML.getValue(e, "latitude"))
                                    , Double.parseDouble(XML.getValue(e, "longitude")),XML.getValue(e, "contact") ));

                     }// for (int i = 0; i < nodes.getLength(); i++)
                     db.close();
                     Toast.makeText(getApplicationContext(), "Database Successfully Updated", Toast.LENGTH_LONG).show();
                     displayLocations();
           }
       }
   };

Then call updateHandler from where you want to run the thread

int get_update_data = -999;
            Message theMessage = updateHandler .obtainMessage(get_update_data);
            updateHandler.sendMessage(theMessage);//Sends the message to the UI handler.
0

There are two Solution of this Problem but i will suggest you to use first solution because second solution is only for hide the problem.

1) Don't write network call in Main UIThread, Use Async Task for that.

2) Write below code into your MainActivity file after setContentView(R.layout.activity_main);

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

And below import statement into your java file.

import android.os.StrictMode;
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
-1

from android 2.3 version strict mode developed, it wont allows data like xml files from server, then you can add some code.

    if (android.os.Build.VERSION.SDK_INT > 8) {

                           StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                           .permitAll().build();
                           StrictMode.setThreadPolicy(policy);

                   }

if you can add the above code then you can check in 4.0 version mobile, it will work happy.
Mohan
  • 651
  • 4
  • 13