ArticleDirectory
- // Start the thread to execute the download task
Recent testsProgramIf the test is normal on the mobile phone end, the exception Android. OS. networkonmainthreadexception will always be thrown during Internet connection.
That is to say, you cannot perform online operations in the main thread.
In 4.0, the access network cannot be carried out in the main program. There are two solutions: one is to add in the main program:
Strictmode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.
// For details, see the strictmode document.
Strictmode. setthreadpolicy (New strictmode. threadpolicy. Builder ()
. Detectdiskreads ()
. Detectdiskwrites ()
. Detectnetwork () // or. detectall () for all detectable Problems
. Penaltylog ()
. Build ());
Strictmode. setvmpolicy (New strictmode. vmpolicy. Builder ()
. Detectleakedsqlliteobjects ()
. Detectleakedclosableobjects ()
. Penaltylog ()
. Penaltydeath ()
. Build ());
The other is to start the thread to execute the download task:
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Start the thread to execute the download task
New thread (downloadrun). Start ();
}
/**
* Download thread
*/
Runnable downloadrun = new runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
Updatelistview ();
}
};