Android-tips (Practical Android Development Tips)

Source: Internet
Author: User

Finishing my actual development encountered some problems and solutions and some development skills, will not be updated regularly.

tips:利用“目录”可快速导航
1. Trace the history of one of the classes in the SDK with the increase in the SDK version. (Find API changes)

Source: Swiperefreshlayout Source: Determine if the sub-view can scroll up (or whether to scroll to the top):

    /** * @return Whether It is possible for the child view of this layout to * scroll up.     Override this if the child view is a custom view. */     Public Boolean Canchildscrollup() {if(Android.os.Build.VERSION.SDK_INT < -) {if(MtargetinstanceofAbslistview) {FinalAbslistview Abslistview = (abslistview) mtarget;returnAbslistview.getchildcount () >0&& (Abslistview.getfirstvisibleposition () >0|| Abslistview.getchildat (0). GetTop () < Abslistview.getpaddingtop ()); }Else{returnViewcompat.canscrollvertically (Mtarget,-1) || Mtarget.getscrolly () >0; }        }Else{returnViewcompat.canscrollvertically (Mtarget,-1); }    }

Go to the Android development website, if you want to see changes in the View API, enter view, select android.view.View ,

Go to the View's API Reference page (document page),

You can see three main messages:

    • View is added at API Level 1
    • Class hierarchy of view
    • With the API level choose button on the left, you can view the view API at different API level for portrait viewing, and you can compare the view API at different API level horizontally.

Traceability of API changes is achieved through the third article above. For example, we want to see what happens between API level 13 and API level 14, set the left API level to 13, and see a list of methods:

We will find some API is gray, when the mouse hover the method name, will show a hint,

This tip tells us that the method in view canScrollVertically(int direction) is only added after API level 14, and canScrollHorizontally(int direction) is also a method added after API level 14. When I switched the API level to 14 o'clock, I found the color of the two methods turned blue, which means they were added at API level 14:

Summary:

The advantage of using this method is that you don't have to download the source code for each API version, or you can easily compare the changes between them. Development reference In addition to the changes can be compared to the method, but also to compare the internal class, interface and other changes, the currently selected API level of 9, the result

2. Use the method profiling tool in Device Monitor to find the culprit for app lag

Problem Source: When using Rxjava, there are inexplicable lag, the method nesting too deep, or the class relationship is too complex, difficult to locate the problem.

Go to the Sdk->tools folder. Double-click Run to monitor.bat open device monitor:


On the left Device tab is the current device name and the package name of the app to be debugged. Before you test an action (method call), click the Method Profiling button and the dialog box pops up:

Input sampling interval:

Input sampling interval, the larger the interval, the probability of acquiring a method call stack, the smaller the likelihood of missing a call stack, the smaller the sampling accuracy (or the higher the coverage) but the sampling interval is too small, will lead to Kaka. Therefore, a suitable sampling interval needs to be entered. After entering the confirmation, and then performing your operation on the app, after executing the point stop method profiling, will generate a file called "ddms+ timestamp +.trace", which records the method of the call stack information, this file is the focus of our analysis:

The previous column in the view shows all the threads or processes that were executed in parallel during method profile. The table in the next column shows the method's invocation information, such as the method name, elapsed time, CPU usage, and so on. Refer to: http://blog.csdn.net/androiddevelop/article/details/8223805

Description of the related column names in the table:


Clicking the name of each column in the table can be sorted, according to the method called by name, such as Fetchdata:

The parents is worth the Fetchdata call entry, and children refers to the child invocation in the Fetchdata method.
You can see the time-consuming situation of each child call or the parent call, you can see that the child calls in Fetchdata Fetchdataimpl time consuming 22.777ms, continue to click Fetchdataimpl can enter Fetchdataimpl call stack, The analysis method is the same as fetchdata, in this way can locate the source of time-consuming operation:

Summary:

Using method profiling, you can analyze the call stacks of the methods to find the app's performance bottlenecks. Android device monitor is powerful, is a collection of tools, can also analyze heap dump,allocation,network, view device files, and so on. Good tools can improve development efficiency, scientific use of tools can do more with less. Also recommended an online app cold start performance analysis tool: Nimbledroid can analyze the app cold-start call stack, through which he can optimize the app's boot speed, and can learn other apps to speed up the way to speed up your app to improve the startup speed.

Later will not be updated regularly, not to be continued.

Android-tips (Practical Android Development Tips)

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.