Sparsearray There are many places from Performance Optimizer that uses Sparsearray to replace HashMap to save memory and improve performance.
Linkify.addlinks () This class makes it easier to add hyperlinks to text.
Thumbnailutils This class is mainly used to deal with thumbnail-related, have had this aspect of demand, should have used this class.
Bitmap.extractalpha (); Returns the alpha value of a new bitmap,capture original picture. Sometimes we need to dynamically modify the background image of an element and do not want to use more than one picture, through this method, combined with canvas and paint can dynamically modify a solid color bitmap color.
Static variables do not directly or indirectly refer to activity, service, and so on. This uses activity and all the objects it references cannot be freed, and then the user takes a long time to skyward the memory.
One feature of the handler mechanism is that it does not end with the life cycle of the activity and service. That is, if you post a delay of runnable and then exit activity,runnable to the time after the runnable executes, it will be executed. If the runnable contains an update view operation, the program crashes.
Many people like to use context.runonuithread when updating view in a sub-thread, this method has a disadvantage, that is, one but the end of the context life cycle, such as the activity has been destroyed, a call will crash.
Packagemanager.getinstalledpackages This method is often used, you may not know that when the number of results obtained is more, in some models call it to spend the time may be second-level, so try to use in the child thread. In addition, if the result is too much, more than the system set of binder data maximum transfer limit, it will occur transactionexception, if you use this method to get a list of installed applications on the machine, it is best to do a precaution.
If you use context.startactivity to start an external application, it's a good idea to do some exception prevention because an exception is thrown when the corresponding application is not found. If you are opening an in-app activity, do not use explicit intent, which can improve the efficiency of the system's search target activity.
Determine if the phone is not flying mode Boolean isenabled = Settings.System.getInt (Context.getcontentresolver (),
Settings.System.AIRPLANE_MODE_ON, 0) = = 1;
If a subclass implements the Serializable interface and the parent class is not implemented, the parent class is not serialized, but the parent class must have a parameterless constructor, or the invalidclassexception exception will be thrown.
The transient keyword modifier variable can limit serialization.
Call order between class inheritance parent class static member--subclass Static member--parent class normal member initialization and initialization block--parent class constructor Method--subclass normal member initialization and initialization block--subclass constructor method
Huawei Mobile Phone cannot display the log solution. Dial-up interface input (##2846579 ##) Service menu will appear. Go
To "Projectmenu", "Background Setting", "Log Setting" Open "log Switch" and set it to on. Open "Log Level setting" and set the Log level wish.
The background service often passes the parameter null in the presence of Onstartcommand (), such as a restart, by changing the return value in Onstartcommand () to return Super.onstartcommand ( Intent,service.start_redeliver_intent, Startid); can solve the problem. Here are the meanings of several flags:
GetWindow (). setlayout (ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT); Set the full-screen method must be after Setcontentview
Viewpager's setcurrentitem must be called after the Setadapter method to be effective.
The current activity's OnPause method executes after the execution of the next activity OnCreate method, so in the OnPause method is not suitable for long-time work, which will affect the efficiency of the jump between pages;
Using Android's transparent theme with caution, transparent themes can cause a lot of problems, such as: if the new activity uses a transparent theme, the current activity's OnStop method is not invoked, and when you press the home key in the activity interface set to transparent theme, May result in the brush screen is not clean problem, into the theme of the transparent theme of the interface will have a noticeable delay sense;
Do not initialize viewstub in a non-UI thread, otherwise it will return null;
Try not to cache data through application, which is not stable: do not cache data in Android application objects!
(Animationdrawable in Android5.0 and above the version has been significantly optimized) try not to use the animationdrawable, it in the initialization of all the pictures loaded into memory, especially memory, and can not be released, released after the next step into the load will be error;
Careful use of Android multi-process, although the multi-process can reduce the memory pressure of the main process, but will encounter the following problems: (1) can not fully exit all activity function (if there is a peer in the application of multi-process successful implementation of the complete exit program Welcome to communicate) ; (2) The first time to enter the new launch process of the page will have a delay phenomenon (it is possible to black screen, white screen a few seconds, is the white screen or black screen and the theme of the new activity); (3) when the process is applied, the new START process will run again application OnCreate method, Do not re-create a application, but will rerun the application OnCreate so that the data can not be cached in application as a way to share memory ; (4) It is not stable to share data between multiple processes through sharedpreferences, and can refer to "Android Development art exploration" in detail.
The larger the area of the view is, the longer the drawing time, and the transparent channel has a great influence on the drawing speed of the view;
When doing custom handwriting function, the underlying escalation point will not be in the motionevent in time to receive, such as the bottom of a second 200 points, the upper layer may receive only dozens of points, in order to improve the fluency of handwriting, in Ontouchevent, Through the motionevent in the gethistorysize can be obtained from the bottom to the upper layer of the process of all points;
After Android4.0, in the manifest.xml static registration of the broadcast, the program must be started once installed to receive the broadcast, such as your application monitoring the start of the broadcast, you must be running the program to be heard;
Before 2.3 GC operation can not be carried out concurrently, that is, the system is in the GC program can only block the waiting GC end, after 2.3, the GC operation changed to a concurrent way, the GC process will not affect the normal operation of the program, but the start and end of the GC operation will be briefly blocked for a period of time. So frequent GC will cause the use of the process of the application, so in order to apply in the process of smoother, need to minimize the triggering of GC operations, which involves performance optimization, for static code analysis, as is already very powerful, you can use Android Studio's Analyze→inspect Code ... for analysis;
In terms of string handling, the Android.text.TextUtils class is capable of working with the most public static Boolean isEmpty (@Nullable charsequence str) This method determines that the string is non-null and NOT NULL
public static charsequence concat (Charsequence ... text) string connection
public static Boolean Equals (Charsequence A, charsequence b) determines whether the contents of two string objects are equal, and non-null judgments are made internally
public static string HtmlEncode (string s) transcoding special characters in HTML code
When judging some commonly used URIs, you can use the Android.webkit.URLUtil tool class
Android.text.format.DateUtils This class is a tool class for the format date provided by Android.
public static String FormatDateTime (context context, long millis, int
Flags); This method can format the date and time, receiving three parameters, where the third parameter, flags, determines the output format, if only the date is output:
Dateutils.formatdatetime (Context,millis,dateutils.format_show_date)
public static Boolean IsToday (long when) determines whether the date is today
public static Charsequence getrelativetimespanstring (Long StartTime)
Returns a string with the current relative time, such as the time of the previous message displayed on the chat list, if it is today's time, yesterday's show yesterday, and a little longer to show the date (but the maximum of this method can only be accurate to the day, not as shown to a few seconds ago, a few minutes ago).
Tools Schemas When you create a new activity, the IDE adds the tools namespace by default in the root node of the XML
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayoutxmlns:android= "http://schemas.android.com/apk/Res/Android"Xmlns:app="Http://schemas.android.com/apk/res-auto" Xmlns:tools="Http://schemas.android.com/tools" Android:layout_width="Match_parent" Android:layout_height="Match_parent" android:orientation="Vertical" Tools:context=". Mainactivity " Tools:showin="@layout/activity_main_utils"> <buttonandroid:id= "@+id/btn_select_date"android:layout_width=" Match_parent "android:layout_height=" Wrap_content "</linearlayout>
when writing layouts, it is often useful to write useless strings to preview the layout, but the data is usually deleted after completion, sometimes in order to make it easier to preview and write when maintaining. You can then use tools ' namespaces, such as tools:text= "click to select Date" tools can only preview and ignore this property when parsing a layout file in a running state. This is ideal for previewing layouts.
If it is within the scope of the application broadcast can use Localbroadcastmanager this API (low version with V4), more secure and efficient, do not have to worry about other apps to fake the broadcast or listen to your radio do some bad things.
Html.fromhtml () If you are familiar with HTML, you can quickly handle some rich text operations in this way. such as hyperlinks and graphic typesetting and other processing.
Build.version_codes There are times when our app needs to perform different actions based on different SDK versions
Valueanimator.reverse () smooth cancellation of animation effects
Formatter.formatfilesize () This method formats the size of the data, depending on the input byte size, returns B KB MB GB, and so on (max support to PB). Note, of course, that the maximum input value is long.max_value.
Typedvalue.applydimension () First This method we can use to convert the unit between the SP DP and PX
Sometimes can not use application context, otherwise will be error (such as Start activity, show dialog, etc.)
* Note: We notice that there are some numbers added to the number, which in fact is yes in terms of ability, but why no? One of the following explanations: 1. Number 1: Startup activity is possible in these classes, but a new task needs to be created, typically not recommended; 2. Number 2: In these classes go to layout inflate is legal, but will use the system default theme style, if you customize some styles may not be used; 3. Number 3: Allowed when receiver is null, in version 4.2 or later, to get the current value of the sticky broadcast. (can be ignored); 4. ContentProvider, broadcastreceiver in the table above because there is a context in its internal method that uses the
Uncaughtexceptionhandler interface, a good code exception is unavoidable, using this interface can be used to clean up the uncaught exception
A series of tool methods in the arrays class on array manipulation: BinarySearch (), aslist (), Equals (), sort (), toString (), Copyofrange (), etc. A series of tool methods in the collections class about set operations: sort (), reverse (), etc.;
Onwindowfocuschanged (Boolean), Onnewintent (intent) and other callback methods in the Activity class;
Pagetransformer interface, use to customize the Viewpager page to switch animation, using the Setpagetransformer (Boolean,pagetransformer) method to set;
urlquerysanitizer--Use this tool to make it easy to check URLs.
The activityoptions--facilitates the definition of two activity toggle animations. Using Activityoptionscompat can be a good solution to older versions of compatibility issues.
GetParent (). Requestdisallowintercepttouchevent (true); denies the parent view the right to handle touch events
Intentservice, a service that can kill himself and not need us to manage a child thread.
Handlerthread, instead of the repetitive physical notation of the thread of the new thread.
Android:animatelayoutchanges= "true", the way to add view animations in LinearLayout, supports custom animations through Setlayouttransition ().
Asyncqueryhandler, if you do the development of system tools, such as contact SMS AIDS, and so on, must inevitably and contentprovider deal with, if the amount of data is not very large case, casually engage, if the volume of data is large, it is necessary to understand this class, It is important to note that this thing eats abnormally.
The Android:descendantfocusability,listview item in the checkbox and other elements grab focus causes the item click event to be unresponsive, in addition to the corresponding element settings
Focusable, it is simpler to add android:descendantfocusability= "blocksdescendants" to the item root layout.
Includefontpadding= "false", textview default up and down is a certain padding, sometimes we may not need to leave the upper and lower part of the white, plus it can.
Messenger, when interviewing, is usually asked about inter-process communication, in addition to Aidl, and this can be installed in ~ ~ (Android Development Art exploration)
Edittxt.setimeoptions, when you use EditText to eject the soft keyboard, modify the display of the ENTER key.
Java8 in the new Localdate and LocalTime interface, although the date is a universal interface, but it is really bad, with these two, finally can happily deal with the date time.
Weakhashmap, direct use of hashmap sometimes poses a risk of memory overflow, using Waekhashmap to instantiate a map. When the user no longer has an object reference, Weakhashmap will automatically be removed from the object that corresponds to the key value.
When using Snackbar, do not use View.getrootview () as the Snackbar view, Huawei Glory 7 will be a problem.
Set TextView single-line display do not use Lines=1, and to use singleline= "true", because the Meizu part of the phone in the setting of Lines=1, and then textview the value of all the numbers, you will be confused.
Argbevaluator can be used to calculate the interpolation between different color values, with the use of Valueanimator.ofobject or viewpager.pagetransformer, you can achieve a smooth transition between different colors.
Activity.recreate re-create the activity. What's the use of it? You can refresh the current activity immediately after the program has changed the theme, without the obvious animation of restarting the activity. (The screen will turn black)
View.getcontext as the name implies, you don't have to explain.
View.post makes it easy to modify the interface in a non-UI thread, similar to the role of handler. And since Post's runnable will be guaranteed to be called only when the view is finished, it can also be used to get the width of the view, and the other is to get the wide height with getviewtreeobserver.
Fragment.setuservisiblehintfragment can override this method, and then perform some logic based on the Boolean value of the parameter (true to indicate that the current Fragment is visible to the user).
Android:cliptopadding
Sets whether the parent view allows its child view to be drawn in its padding (this refers to the padding of the parent view). Isn't it a little bit around? For example: If there is a ListView, we want to be in the initial position, the first item from the top of the 10DP distance, you can add android:cliptopadding= "false" Android in the ListView layout: Paddingtop= "10DP".
Ontrimmemory, overriding this method in activity, will call back (support multiple levels) when memory is tight, so that we can proactively release resources and avoid oom.
Android:clipchildren (ViewGroup)--If this property is set to unavailable, then ViewGroup's child view will be drawn out of its range, which is required when animating.
Android:tilemode (bitmapdrawable)--You can specify the pattern in which the picture uses a repeating fill.
Sharedpreference.editor Apply is an asynchronous operation that does not return a successful state, and commit is a synchronous operation, so when multiple concurrent commit commits, they wait for the commit to be processed to disk before the next data is manipulated, thereby reducing efficiency.