Android development interview: 6. FAQs from common interviewers: Android question ② (updating...), android updated

Source: Internet
Author: User

Android development interview: 6. FAQs from common interviewers: Android question ② (updating...), android updated

Follow finddreams blog: http://blog.csdn.net/finddreams/article/details/44560061
1. What are their respective advantages and disadvantages of HttpURLConnection and HttpClient?
HttpUrlConnection versions earlier than 2.3 have bugs. Therefore, HttpClient is recommended for earlier versions, but google does not maintain HttpClient now, And HttpClient has expired in version 5.1. In addition, HttpURLConnection supports gzip compression. We recommend that you use it first.
Before Froyo (2.2), there was a major Bug in HttpURLConnection. Calling the close () function will affect the connection pool, resulting in invalid connection reuse. Therefore, keepAlive needs to be disabled when using HttpURLConnection before Froyo.
In addition, gzip compression is enabled by default in Gingerbread (2.3) HttpURLConnection to improve HTTPS performance. Ice Cream Sandwich (4.0) HttpURLConnection supports request result caching.
In addition, the HttpURLConnection API is relatively simple. Therefore, for Android, we recommend that you use HttpURLConnection after 2.3. We recommend that you use AndroidHttpClient before.

2. What are the advantages and disadvantages of XML parsing methods in Android development?
DOM, SAX, and Pull parsing.
The advantage of the SAX Parser is that the parsing speed is fast and the memory usage is small;

The DOM is stored in the memory in a tree structure, so the retrieval and update efficiency is higher. However, for large documents, parsing and loading the entire document will consume a lot of resources and is not suitable for mobile terminals;

The PULL parser runs in a similar way as SAX and is based on the event mode. The PULL parser is lightweight, fast in resolution, and easy to use. It is very suitable for Android mobile devices, the Android system also uses the PULL parser to parse Various XML files.

3. How do you achieve multi-resolution adaptation during normal development?
1. Create different layout files based on different resolutions
2. Create resource images with different resolutions based on different resolutions
3. When the program starts, obtain the resolution and density of the current screen and adapt it to the code.
4. Write different dimen files for different resolutions.
5. In fact, more fragement is used.

4. How did you solve a bug at work?
1. view Log
2. breakpoint debugging if the Log cannot be solved
3. If debug fails
4. Log. e ("error", "1") near the Exception Code;, 6, 8, 9, output a Log in every row.
5. Locate the problem and find your own ideas. If it is a technical bottleneck, google

5. Why is the static keyword recommended when declaring the internal class of ViewHolder?
In fact, this is one of the main differences between static and non-static internal classes. Non-static internal classes implicitly hold external class references, just as you often put custom adapters in the Activity class, in the adapter class, you can call the external activity method at will. When you define an internal class as static, you cannot call the instance method of the external class, because the static internal class does not hold the reference of the external class at this time. Declare the static internal class of ViewHolder. You can dereference ViewHolder and the external class. Generally, ViewHolder is simple. It's okay if you don't define it as static. This is true, but if you define it as static, you understand these meanings. If one day you add some complicated logic to this ViewHolder and perform some time-consuming work, if ViewHolder is a non-static internal class, memory leakage may easily occur. If it is static, you cannot directly reference external classes, forcing you to focus on how to avoid mutual references. Therefore, defining the internal class of ViewHolder as static is a good habit.
Non-static internal classes implicitly hold strong references of external classes, but may cause memory leakage. In general, using viewhodler will not cause memory leakage. Adding static is a good habit.

6. How can I display a ultra-high-definition image or long image without losing sight of the truth?
1. computation BitmapFactory. Options to compress the image by calculating the inSamleSize value of the BitmapFactory. Options object.
2. Use WebView to load the image;
3. Use MapView or TileView to display images (similar to the map mechanism );

7. What methods does Android provide for scheduled and delayed tasks? What are their application scenarios?
Countdown
CountDownTimer

Latency
CountDownTimer Can skillfully set countDownInterval to be the same as millisInFuture, so that onTick and onFinish are called only once.
Handler. sendMessageDelayed. For more information, see the internal implementation of CountDownTimer.
TimerTask: the code is messy.
Thread. sleep.
Use the Handler method postDelay (runnable, delayTime)

Timing class
For the latency class, calculate the delay time.
Handler. sendMessageAtTime
AlarmManager, suitable for long-term scheduled times, such as alarm

8. Talk about your understanding of StrongReference, WeakReference, and SoftReference.
StrongReference: this is a common reference in code, such as Object obj = new Object (). As long as a strong reference still exists, GC Will Never recycle referenced objects.
SoftReference: used to describe some useful but not necessary objects. For soft reference associated objects, when the system is about to encounter a memory overflow exception, these objects will be included in the recycle range for the second recycle. If the recovery does not have enough memory, a memory overflow exception will be thrown. After JDK 1.2, the SoftReference class is provided to practice soft references.

WeakReference: it is also used to describe non-essential objects, but its strength is weaker than soft references. objects associated with weak references can only survive until the next GC occurrence. During GC, objects associated with weak references will be recycled no matter whether the memory is sufficient at the time. After JDK 1.2, the WeakReference class is provided to implement weak references.

PhantomReference. A Virtual Reference is also called a ghost reference or phantom reference. It is the weakest reference relationship. Whether an object has a virtual reference does not affect its survival time, nor can it be used to obtain an object instance through virtual reference. The only purpose of setting a virtual reference for an object is to receive a system notification when the object is recycled by GC. The PhantomReference class is provided after JDK 1.2 to Implement Virtual Reference.

9. How is the network layer of your application designed?
1. android-async-http.
It encapsulates common methods, get post upload and download, and I use synchronous requests for all requests.
The specific usage is generally associated with the business logic, and my business logic is processed asynchronously.
The cache of network request results is handled independently and not on the network layer.

2. It is encapsulated on the basis of HttpUrlConnection, including request success, failure, request, and network problems. Broadcast and UI interaction are used.
3. directly use open-source third-party frameworks such as xUtils, afinal, okHttp, and Volley;

Bitmap is a frequently used class in android, which represents an image resource.
Bitmap consumes a lot of memory. If you do not pay attention to the Optimization code, the OOM problem often occurs. There are several optimization methods:
1. Use cache;
2. compress the image;
3. Timely recovery;

10. Let's talk about your understanding of Bitmap. When should I manually call bitmap. recycle ()?
As to the time when recycle needs to be manually called, it depends on the specific scenario. The principle is that Bitmao needs to be recycled when we no longer use Bitmao. In addition, we need to note that Bitmap objects and pixel data are stored separately before 2.3, Bitmap objects exist in java Heap, and pixel data is stored in Native Memory, in this case, it is necessary to call recycle to reclaim the memory. However, after 2.3, Bitmap objects and pixel data are stored in Heap, and GC can recycle its memory.

11. How can I optimize loading Fragment in ViewPager? How does one achieve delayed loading during interface switching?
The setUserVisibleHint method in fragment can be used.

private boolean hasLoadedOnce = false; // your boolean field@Overridepublic void setUserVisibleHint(boolean isVisibleToUser) {    super.setUserVisibleHint(isVisibleToUser);    if (this.isVisible()) {        // we check that the fragment is becoming visible        if (isVisibleToUser && !hasLoadedOnce) {                //do something            }        }    }}

12 What is aar? What is the difference between aar and jar?
The "aar" package is the binary release package of the Android class library project.

The file extension is. aar, And the maven project type should also be aar, but the file itself contains the following zip files:

/AndroidManifest. xml (mandatory)
/Classes. jar (mandatory)
/Res/(mandatory)
/R.txt (mandatory)
/Assets/(optional)
/Libs/*. jar (optional)
/Jni // *. so (optional)
/Proguard.txt (optional)
/Lint. jar (optional)
These entries are directly located in the root directory of the zip file.
In this example, the r.txt file is the output result of aapt with the-output-text-symbols parameter.

Jar packages cannot contain resource files, such as drawable files and xml resource files. aar can.

13. How can I protect URLs from being hacked?
Encryption to JNI will still be captured through the packet capture tool. The final method is to perform two-way encryption verification for HTTPS certificates

14. Differences between Android fragment and activity
You can understand that Fragment is a special View that represents a module or a special part.
Most Fragment relies on the existence of the Activity and is managed by the FragmentManager of the Activity.
Fragment can solve the problem of multiple activities, that is, change frequent Activity jumps before 3.0 to the Failover of Fragment within an Activity.
Fragment can solve the fragmentation problem.
Fragment is added in android3.0.
Fragment can be reused
Fragment must be nested in the activity, and its lifecycle is affected by the activity.

15. Will ANR appear in Service and broadcast BroadcastReceivre?
Service, ANR will appear in the broadcast
Services and broadcasts are all in the main thread. Since it is the main thread, of course it will be anr, so time-consuming operations must start with another thread.
Generally speaking, the timeout time is 5 seconds for Activity, 10 seconds for Broadcast, and 20 seconds for Server.

16. Which design patterns do you use during normal development? Can you talk about the application scenarios of these design patterns?
Usually, the singleton mode (used when only one object is instantiated in the memory), the adapter mode (typically the ListView and GridView adapters), and the builder mode (AlertDialog. builder), the observer mode may be relatively hidden. In the Android source code, implement the NotifyDataSetChanged of BaseAdapater (?)
Singleton: DownloadManager

Related Article

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.