1. What is the strict mode (Strictmode)
Strictmode is a development tool that detects what you might do by accident and lets you focus so that you can fix them.
Strictmode is the most commonly used disk or the main thread of an unexpected network access application, UI actions and animations are received. Keeping the main thread of your disk and network business smoother and your application more sensitive. You also prevent the ANR dialog box from appearing to the user by keeping the application's main thread responsive.
Note that even though the disk of the Android device is usually in Flash, many devices running memory on the file system have very limited concurrency. It's often the case that almost all of the disk accesses are fast, but in individual cases it can slow down significantly when some I/O occurs in the background from other processes. If possible, it is best to assume that such things are not unpleasant.
2. Usage of harsh mode
Google's example is as follows:
public void oncreate () {if ( Developer_mode) {strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builde R (). Detectdiskreads (). Detectdiskwrites (). Detectnetwork () //or. Detectall () for all detectable problems . Penaltylog (). Build ( )); Strictmode.setvmpolicy (new StrictMode.VmPolicy.Builder (). DETECTLEAKEDSQ Lliteobjects (). Detectleakedclosableobjects (). Penaltylog (). Penaltydeath ( ). Build ()); } super . OnCreate (); }
Just add this sentence to the Super.oncreate () method.
3. Optimize the tough mode to make it a real tool
We can write BaseActivity in, let the other Activity inherit from this BaseActivity , and set a switch to control itDEVELOPER_MODE
4. View Log Day Information
Let's look at the code below.
/** * Created by ZSL on 2014/12/31. * * Public class strictmodeactivity extends baseactivity implements baseactivityinterface,View. Onclicklistener { @Override protected void onCreate(Bundle savedinstancestate) {Appconfig.setdevelopermode ();Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); Initevent (); InitData (); }@Override Public void Initview() { }@Override Public void initevent() { }@Override Public void InitData() {String url="http://g.hiphotos.baidu.com/image/w%3D310/sign=7725d8ccd3a20cf44690f8de46084b0c/ E1fe9925bc315c601a0f34a48eb1cb13485477e6.jpg ";Try{URL URL1 =NewURL (URL); HttpURLConnection connection = (httpurlconnection) url1.openconnection (); Connection.setconnecttimeout (15000); Connection.setreadtimeout (10000); Connection.setrequestmethod ("GET"); Connection.setdoinput (true); Connection.connect (); Connection.getinputstream (); }Catch(IOException e) {E.printstacktrace (); } }@Override Public void OnClick(View v) {Switch(V.getid ()) { Case 1:{ Break; }default:{ Break; } } }}
I am in the main thread to directly access the network, under normal circumstances, if we do so, the network is in good condition, there will be no impact.
But what will be the effect of looking at it after we add a rigorous model?
Log days will be error, and Strictmode keywords appear, and will be the cause of the errors and areas needing improvement
Finally, a non-clickable interface will be re-popped on the emulator, which means you need to do some optimizations
5. Summary
What does the rigorous mode check?
Check for time-consuming operations on the main thread: network access, complex animations, disk reads and writes, etc.
Google rigorous mode "debug, optimize, check" to make your app better, breakthrough optimization bottlenecks