Today is Feng Jian’s contribution, summarizing his experience in Android development, I think it is very meaningful, the so-called rich experience sometimes refers to the accumulation of these small experience, I believe his share will help some friends. I have also recommended his APK devil slim one article, have not seen the friend can also go to see again.
Do Android for a long time, will step on a lot of pits, pit more experience, spare time to sort out the part, now select some important or partial door “small” experience to make a record.
View SQLite logs
Log.isloggable (TAG, log.verbose) is used in LessCode LogLess: https://github.com/openproject/LessCode/blob/master/lesscode-core/src/main/java/com/jayfeng/lesscode/core/LogLess.java
Using this method, you can view the print logs of your application even in the Release version.
PNG optimization
APK packaging automatically compresses PNG in a lossless manner, if self – compression is not effective. Of course lossy compression is possible: https://tinypng.com/
Tcpdump caught
Some simulator genymotion bring the tcpdump, for example, if not, need to download the tcpdump: http://www.strazzere.com/android/tcpdump
Push tcpdump to /data/local
See the signature
Many developer services require binding signature information, which can be viewed with the following command:
Note that this requires a password, see MD5, SHA1, SHA256, etc.
The best way to write the singleton pattern (lazy)
In particular, because there’s a lot of code on the web that looks like this:
This is not safe for threads. Add a synchronization lock:
There are more such codes on the Internet, which can work well, but the disadvantage is low efficiency.
In fact, the volatile keyword was introduced as early as JDK1.5, so there is a better way to write double check locks:
This is the best way to write it!!
This is not to say that there is a problem with the second method, or that you should always use the third method in Android, but just to show you a better way to write it.
More process Application
How often do you find that a method in an Application executes multiple times? Puzzled.
Because the Application will execute multiple times when there are multiple processes, you can use pid to determine which methods are executed only once to avoid wasting resources.
Implicitly start the Service
This is a change from Android5.0 that does not support implicit Service calls. Service Intent must be explicit
Can be changed as follows:
The life of the fill_parent
After Android2.2, match_parent is supported. Is your layout file confusing with fill_parent and match_parent?
If your current minSdkVersion is 8+, you can omit fill_parent and use match_parent, otherwise use fill_parent.
Partial refresh of ListView
Some lists may be notifyDataSetChanged() too expensive, and it is best to refresh them locally.
The point of a partial refresh is to find the View of the item to update and then update the data according to the business logic.
Again, don’t forget to update the last list data, otherwise the data source will not change, a scroll may restore.
Several important tags in system logs
TextView with one line in the center and multiple lines to the left
This is usually used in message dialog, if the text is one line, it is centered, and if the text is multiple lines, it is left. The wrap_content ViewGroup layer on the TextView layer can be easily implemented:
setCompoundDrawablesWithIntrinsicBounds()
There are a lot of problems with setCompoundDrawables() methods that don’t show up, and then the solution is setBounds, which needs to calculate the size…
Don’t bother with setCompoundDrawablesWithIntrinsicBounds () method is the most simple!
Calculate the program running time
To calculate the running time of a piece of code, it is common to add startTime to the front of the code and subtract startTime from the current time at the end of the code. The time difference is the running time.
Here’s an easier way to write it, no time logic at all, just add a print log.
There’s no logic to time. Can you measure that?
To filter out logs, run the command”adb logcat -v time | grep TAG“:
Using the -v time parameter, you can compare the time on the left side of the log to figure out how long the middle code ran.
Overview of Java reference types
Context Usage Scenario
To prevent activities, services, and other contexts from leaking into longer-lived objects, you can use longer-lived ApplicationContext, but not all contexts can be replaced with ApplicationContext
Here’s a form circulating on the Internet:
Image cache size
Many image libraries now require a maximum cache for images, but what is the appropriate value?
The configuration of high-end and low-end machines should obviously be different, so you can consider setting a dynamic value.
The recommended value is 1/8 of the available memory of the application:
Some built-in tool classes
AOSP class Util class Util class Util class Util class Util class Util
With so many tool classes, you can definitely find something useful for you.
ClipPadding
If you set ClipPadding to false, you can set the ListView to a variety of padding without any ugly sliding “forbidden zones”.
Powerful dumpsys
Dumpsys can view system services and status, which is very powerful, by viewing all supported subcommands as follows:
Here are a few slightly common ones:
Bugreport command
Many people have used ADB Logcat, but if you want more detailed information, Logcat can’t do it.
So most mobile vendor tests use adb Bugreport to capture logs for developers to analyze.
Dpi folder conversion ratio
Update media library files
When making ROM before, I often encountered some third-party software (some music APP) after downloading new files or deleting files, but the media library did not update, because this is the need for third-party software to initiate.
The media library will be scanned completely when the phone is turned on and the SD card is plugged in and out, which is not real-time and expensive, so it is necessary to refresh individual files.
Monkey parameters
If you run the monkey too fast, it’s not practical. If you run the Monkey too slow, it’s not practical.
Run monkey and catch the log again.
summary
Experience, big or small, is good and works wonders when it matters.
This article is an original article and is forbidden to be reproduced without permission
If you like this article, please help to forward it. Click to read the original article and visit the author’s blog, jayfeng.com