Android.os.NetworkOnMainThreadException after 4.0 Google forced the connection network cannot be accessed on the main thread

Source: Internet
Author: User

Google banned network access in the main thread after the 4.0 system because:

The main thread is the response to the UI, and if the main thread makes network access for more than 5 seconds, it will cause a forced shutdown.

So this time-consuming operation cannot be placed in the main thread. is placed in a sub-thread, and the thread is not able to change the UI of the main thread,

Therefore, it leads to the handler, which is defined in the main thread, handler, and used in sub-threads.

The wording of the message mechanism
[1] ANR Application not response application unresponsive main thread (UI thread)
[2] If a time-consuming operation in the main thread (such as connecting the network copy large Data) Threa.sleep ()
[3] Avoid the ANR can put the time-consuming operation into the child thread itself in the creation of a thread

[4] After 4.0, Google forced the connection network can not be accessed in the main thread
Android.os.NetworkOnMainThreadException
  
[5] Only the main thread (UI thread) can update the UI
Android.view.viewrootimpl$calledfromwrongthreadexception:only
The original tread that created a view hierachy can touch its view.

[6] Information mechanism

Workaround: Open a child thread

        New Thread () {            @Override            publicvoid  run () {                //  TODO auto-generated method Stub                //  inside Write network connection What's the   process of                Super. Run ( );            };        }. Start ();

Android.os.NetworkOnMainThreadException after 4.0 Google forced the connection network cannot be accessed on the main thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.