Download programs designed on Android 2.3 run on Android 4.0 and report to Android. OS. networkonmainthreadexception exception. Originally in 4.0, the access network cannot be performed in the main program. There are two solutions, one being added to the main program:
Java code
- // 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 ){
Java code
- 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 ();
- }
- };