Baidu's Android interview summary analysis

Source: Internet
Author: User
Tags map data structure

Today is today 10 o'clock in the morning, I received a telephone interview Baidu, of course, and I said in advance, my procrastination is how strong, so I did not prepare, of course, I also want to look at their true level, in this review!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!

No more nonsense.

To come up is a technical staff of Baidu called, simple confirmation of my information, began to ask questions, at the moment my heart has 10,000 grass mud horse Pentium .... began to ask is the data structure and algorithm related knowledge, I studied a small point, but not in time to review all forgot ... The next is the relevant knowledge of Android, this part of my answer is the right, there is a question about the ANR, no answer come up ... There is a question about Java, and I have not answered it well.

The topic of the interview
  1. The pre-sequence traversal of binary tree
  2. Simply describe the bubbling algorithm
  3. Android's four components and their functions.
  4. How the service is guaranteed to restart after killing
  5. ANR problem
  6. Generics for Java

    The bad fill of the interview

    I myself went to the major sites to look at, below is my summary of the question and the address of the answer
    Jump between 1.Activity
    http://android.blog.51cto.com/268543/323982
    http://blog.csdn.net/super1900/article/details/4098615
    2.android View Refresh mechanism
    http://blog.csdn.net/dragondog/article/details/6454551
    3. Implement strlen (char* str)
    int strlen (const char *STR)
    {
    if (' + ' = = *str)
    return 0;
    Else
    return strlen (str+1) + 1;
    }
    4. Implement strcpy (char * str)
    The first type:
    void strcpy (char *strdest, char *strsrc)
    {
    while ((strdest++ =strsrc++)! = ' + ');
    Put the first character of the last string assigned value to the address of the first char of the preceding string, and exit if you encounter '. '
    The next string also assigns the ' \ \ ' to the trailing end of the previous string.
    }
    5. Differences in common data structures
    http://blog.csdn.net/ceasar11/article/details/12615225
    Http://www.360doc.com/content/12/1009/16/9400799_240455229.shtml
    6.android WiFi Location
    http://www.zhihu.com/question/21365402
    7.c++ the difference between a reference and a pointer
    http://blog.csdn.net/dujiangyan101/article/details/2844138
    9.Android What is the amount of memory allocated for each application?
    Http://www.cnblogs.com/mythou/p/3203536.html
    16m is also 24m
    10. Sorting Algorithm Summary
    Http://blog.chinaunix.net/uid-25906157-id-3318529.html
    The difference between 11.Integer and int Java
    Http://developer.51cto.com/art/200906/130459.htm
    12.android Aidl
    http://blog.csdn.net/android_tutor/article/details/6427680
    13. Please describe the life cycle of the acitivity?
    http://blog.csdn.net/android_tutor/article/details/6427680
    14. What is the entrance to the Android program?
    http://blog.csdn.net/zhaokaiqiang1992/article/details/25154981
    http://blog.csdn.net/zcmain/article/details/14451591
    What are the 15.Intent features?
    http://blog.csdn.net/dlmu2001/article/details/3478266
    What are the common storage methods for 16.android? If you want to keep the user name and password at login, which is better?
    Http://www.cnblogs.com/hanyonglu/archive/2012/03/01/2374894.html
    17. How does the different sizes of the mobile phone fit?
    http://www.zhihu.com/question/19929388
    18. Optimizing The ListView Control
    http://blog.csdn.net/zhaokaiqiang1992/article/details/33405181

Finally give everyone an Android interview summary, no thanks, there are questions about the Welcome message exchange

1. What are the four components of Android, and what are their roles?
A: Activity:activity is the windows of Android programs interacting with the user, is the most basic of the Android building block, it needs to maintain the state of all walks of life, do a lot of persistent things, properly manage the life cycle and some jump logic
Service: Background services for activity, encapsulation has a complete functional logic implementation, accept the upper instruction, complete the relevant food, define the need to accept the intent provide synchronous and asynchronous interface
Content Provider: is a third-party application data Access program provided by Android, can derive content Provider class, provide data to the outside, can choose sort like database, block the storage details of internal data, provide a unified excuse model, Greatly simplifies upper-layer applications and provides a more convenient way to integrate data
Broadcast receiver: Accepts one or more intent as triggering events, accepts related messages, does some simple processing, transforms into a notification, unifies the Android event broadcast model
2, please introduce the five kinds of layouts commonly used in Android.
There are five ways of layout: Framelayout (frame layout), LinearLayout (linear layout), Absolutelayout (absolute layout), relativelayout (relative layout), tablelayout (table layout).
First, Framelayout: All things in turn in the upper left corner, will overlap, this layout is relatively simple, but also can only put a little more simple things. Second, LinearLayout: linear layout, each linearlayout inside can be divided into vertical layout (android:orientation= "vertical") and horizontal layout (android:orientation= " Horizontal "). When vertical layout, there is only one element for each row, and multiple elements are descending vertically; when horizontal layout, there is only a row, and each element is arranged to the right. Three, Absolutelayout: absolute layout with x, y coordinates to specify the position of the element, this layout is also relatively simple, but in the screen rotation, often problems, and multiple elements of the time, the calculation is more troublesome. Four, Relativelayout: relative layout can be understood as an element as a reference, to locate the layout mode. The main attributes are: relative to an element Android:layout_below, Android:layout_toleftof relative to the parent element Android:layout_alignparentleft, Android: Layout_alignparentrigh, Tablelayout: Table layout, each of the tablelayout inside a table row Tablerow,tablerow inside can define each element concretely. Each layout has its own appropriate way, and these five layout elements can be nested within each other to make an aesthetically pleasing interface.
3. What are the types of animations in Android, and what are their characteristics and differences?
A: Two, one is tween animation, there is also a frame animation. Tween animations, which enable the view component to move, zoom in, zoom out, and create transparency, another frame animation, a traditional animation method, is implemented by sequential playback of a well-arranged picture, similar to a movie.
4. What kinds of XML-parsing classes are in Android? What kind of official recommendation? and their principles and differences.
A: There are three main ways of parsing XML, SAX, DOM, pull. General development on the PC we use the DOM relatively easy, but some performance-sensitive database or mobile phone is mainly sax, sax read is one-way, the advantages: not account for memory space, parsing properties convenient, but the disadvantage is that nested multiple branches for processing is not very convenient. And Dom Way will load the entire XML file into memory, where the Android Development network to remind you that the method in the search aspect can and XPath good combination if the amount of data is not highly recommended, and pull is often used in J2ME for node processing is better, similar to sax mode, Also very memory-saving, in J2ME we often use the Kxml library to parse.
5, the ListView optimization scheme
A: 1, if the custom adapter, then in the GetView method to consider the method passed in the parameter contentview is null, if it is null to create Contentview and return, if not NULL is directly used. Create as few view as possible in this method.
2, to Contentview set tag (Settag ()), passed in a Viewholder object, used to cache the data to be displayed, can achieve the effect of asynchronous loading of image data.
3. If the ListView needs to display a lot of item, you should consider paging load. For example, to show a total of 100 or more times, we can consider loading 20 first, and so on when the user pulls to the bottom of the list to load the next 20.
6. Please describe the data storage method of Android.
A: The use of sharedpreferences storage data, file storage data, SQLite database storage data, the use of ContentProvider storage data, network storage data;
Preference,file, database of the three different ways of the corresponding directory is/data/data/package name/shared_pref,/data/data/package name/files,/data/ Data/package Name/database.
One: Storing data using sharedpreferences
This first describes the Sharedpreferences storage method, which is a mechanism provided by Android to store some simple configuration information, such as the user name and password of the logged-in user. It uses the map data structure to store the information, stored as a key value, can be simply read and write, the concrete example is as follows:
void Readsharedpreferences () {
String Strname,strpassword;
Sharedpreferences user = Getsharedpreferences ("User_info", 0);
StrName = user.getstring ("NAME", "" ");
strpassword = User getString ("PASSWORD", "" ");
}
void Writesharedpreferences (String strname,string strpassword) {
Sharedpreferences user = Getsharedpreferences ("User_info", 0);
Uer.edit ();
User.putstring ("NAME", strName);
User.putstring ("PASSWORD", strpassword);
User.commit ();
}
The method of reading and writing data is very simple, but there are some differences when writing: Call edit () to edit the data before you can modify it, and then commit the modified data using commit (). In fact, sharedpreferences is using XML format to store data in the device, under/data/data//shares_prefs in the File Explorer in Ddms. There are some limitations to using sharedpreferences: They can only be used within the same package and cannot be used between different packages.
Two: File storage data
File storage is a more common method, the method of reading/writing files in Android is exactly the same as the program implementing I/O in Java, providing the Openfileinput () and Openfileoutput () methods to read the files on the device. The concrete examples are as follows:
STRING fn = "Moandroid.log";
FileInputStream FIS = openfileinput (FN);
FileOutputStream fos = openfileoutput (fn,context.mode_private);
Three: Networked storage data
Network storage, need to deal with Android network packets, about the Android network packet details, please read the Android SDK reference to the Java SDK which package?
Four: ContentProvider
1, ContentProvider Introduction
When an app inherits the ContentProvider class and overrides the method used to provide data and store data, it can share its data with other apps. Although the use of other methods can also share data, but the way data access will vary depending on how the data is stored, such as: the use of file-based data sharing, the need for file operations to read and write data, the use of sharedpreferences shared data, You need to read and write data using the Sharedpreferences API. The benefit of using ContentProvider to share data is to unify the way data is accessed.
2. Introduction to URI Class
The URI represents the data to manipulate, and the URI mainly contains two pieces of information: 1. ContentProvider to operate, 2. To manipulate what data is in ContentProvider, a URI consists of the following parts:
1.scheme:contentprovider (content Provider) scheme has been defined by Android as: content://...
2. hostname (or authority): used to uniquely identify this contentprovider, external callers can find it based on this identity.
3. Path: can be used to represent the data we want to manipulate, the path should be built according to the business, as follows:
To manipulate records with ID 10 in the Contact table, you can build such a path:/CONTACT/10
To manipulate the name field of the record with ID 10 in the Contact table, Contact/10/name
To manipulate all records in the Contact table, you can build such a path:/contact?
The data you want to manipulate does not necessarily come from a database, or it can be stored as a file, as follows:
To manipulate the name node under the contact node in the XML file, you can build such a path:/contact/name
If you want to convert a string to a URI, you can use the parse () method in the Uri class, as follows:
Uri uri = uri.parse ("Content://com.changcheng.provider.contactprovider/contact")
3, Urimatcher, Contenturist and Contentresolver Introduction
Because the URI represents the data to manipulate, we often need to parse the URI and fetch the data from the URI. The Android system provides two tool classes for manipulating URIs, Urimatcher and Contenturis, respectively. Mastering their use will facilitate our development efforts.
Urimatcher: Used to match the URI, it uses the following:
1. First put the URI path you need to match all to the registration, as follows:
The constant Urimatcher.no_match represents a return code (-1) that does not match any path.
Urimatcher urimatcher = new Urimatcher (urimatcher.no_match);
If the match () method matches the Content://com.changcheng.sqlite.provider.contactprovider/contact path, a match code of 1 is returned
Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "Contact", 1);//Add need to match URI, if match will return match code
If the match () method matches the content://com.changcheng.sqlite.provider.contactprovider/contact/230 path, a match code of 2 is returned
Urimatcher.adduri ("Com.changcheng.sqlite.provider.contactprovider", "contact/#", 2);//#号为通配符
2. After registering a URI that needs to be matched, you can use the Urimatcher.match (URI) method to match the input URI, and if the match returns a matching code, the match code is the third parameter passed in by calling the Adduri () method, assuming that the match content:// Com.changcheng.sqlite.provider.contactprovider/contact path, the matching code returned is 1.
Contenturis: Used to get the ID part after the URI path, it has two more practical methods:
Withappendedid (URI, id) is used to add the ID portion of the path
The Parseid (URI) method is used to get the ID part from the path
Contentresolver: When an external application needs to add, delete, modify, and query the data in ContentProvider, you can use the Contentresolver class to get the Contentresolver object, You can use the Getcontentresolver () method provided by the activity. Contentresolver uses the INSERT, delete, update, and query methods to manipulate the data.
7. What are the activation modes of activity? What does it mean?
A: In Android, there are 4 activation modes for activity, namely:
"Standard" (default)
"Singletop"
"Singletask"
"SingleInstance"
They are mainly different from the following:
1. How to decide which task to belong to
The target TASK for the ACTIVITY of "standard" and "Singletop", and the sender of the received intent within the same task, unless intent includes the parameter flag_activity_new_task.
If the Flag_activity_new_task parameter is supplied, it is launched into another task.
"Singletask" and "singleinstance" always use activity as the root element of a task, and they will not be launched into one other task.
2. Whether multiple instances are allowed
"Standard" and "singletop" can be instantiated multiple times and exist in different tasks, and a task can include multiple instances of an activity;
"Singletask" and "singleinstance" restrict the generation of only one instance, and are the root elements of a task. Singletop requires that if the stack top already has an instance of the activity to create when the intent is created, the intent is sent to the instance without sending it to the new instance.
3. Allow other activity to exist within this task
"SingleInstance" exclusive of a task, other activity cannot exist in that task; if it launches a new activity, regardless of the new activity's launch mode, The new ACTIVITY will be run in another task (as with the Flag_activity_new_task parameter).
The other three modes can coexist with other activity.
4. Whether to generate a new instance every time
"Standard" for not a start intent will generate a new instance of activity;
The activity of "singletop" if it is at the top of a task's stack, does not generate a new instance of the activity, directly using the instance at the top of the stack, otherwise, generates an instance of the activity.
For example, now the task stack element is a-b-c-d (d at the top of the stack), at this time to send a start intent D, if D is "standard", then generate a new instance of D, the stack becomes a-b-c-d-d.
If D is singletop, a new instance of D will not be produced, and the stack state is still a-b-c-d
If at this time to B hair intent, whether B launchmode is "standard" or "singletop", will generate a new instance of B, the stack status becomes A-b-c-d-b.
"SingleInstance" is the only activity on its stack, which is reused every time.
"Singletask" if at the top of the stack, accept intent, otherwise the intent will be discarded, but the task will still return to the foreground.
When the existing activity instance processes the new intent, the Onnewintent () method is called if the intent generates an activity instance, then the user can return to the previous state via the back key If an activity already exists to handle the intent, the user cannot return to the previous state by pressing the back key.
8. What are the intent startup methods related to activity and task? What does it mean?
The core intent flag is:
Flag_activity_new_task
Flag_activity_clear_top
flag_activity_reset_task_if_needed
Flag_activity_single_top
Flag_activity_new_task
If set, this activity will be the beginning of a new task in the history stack. A task (from its activity to the activity in the next task) defines the group of activity atoms that the user can migrate. Tasks can be moved to foreground and background, and all activity in a particular task is always kept in the same order.
This flag is typically used to render the behavior of a "startup" type: They provide a series of things that a user can do independently, regardless of the activity that started them.
With this flag, if the activity task that is being started is already running, the new activity will not start, instead, the current task will simply move into the foreground. Refer to the FLAG_ACTIVITY_MULTIPLE_TASK flag to disable this behavior.
This flag cannot be used for the result of an activity request that has been initiated by the caller.
Flag_activity_clear_top
If set, and the activity is already running in the current task, it is no longer an instance of the activity being restarted, but all activity above the activity will be closed, The intent will then be delivered as a new intent to the old activity (now on the top).
For example, suppose a task contains these activity:a,b,c,d. If D calls StartActivity () and contains a intent that points to activity B, then both C and D will end, and B receives the intent, so the current state of the stack is: A, B.
Activity B, which is running in the example above, can either receive this new intent in onnewintent () or shut itself down and restart to receive the intent. If its startup mode is declared as "multiple" (the default), and you do not set the FLAG_ACTIVITY_SINGLE_TOP flag in this intent, it will be closed and recreated, and for other startup modes, or set the FLAG_ACTIVITY_SINGLE_TOP flag in this intent, this intent will be posted to the current instance of the Onnewintent ().
This startup mode can also be used in conjunction with Flag_activity_new_task: Used to start the root activity in a task, which takes any running instance of that task into the foreground and clears it until the root activity. This is useful, for example, when an activity is started from Notification Manager.
flag_activity_reset_task_if_needed
If this flag is set, the activity will start with the front door of the task, whether it is starting from a new stack or pushing from an existing stack to the top of the stack. This means that any stack associated with the application is reset to its normal state (whether it is talking about activity moving in or removing), if necessary, or simply resetting the stack to its original state.
Flag_activity_single_top
If set, when the activity is running at the top of the history stack, no longer starts a new
Flag_activity_brought_to_front
This flag is generally not set by the program code, such as the Launchmode set Singletask mode when the system to help you set.
Flag_activity_clear_when_task_reset
If set, this sets a restore point in the activity stack of the task, and when the task resumes, the activity needs to be cleaned up. That is, when the next TASK enters the foreground with the flag_activity_reset_task_if_needed tag (typically the user restarts it on the main screen), the ACTIVITY and it will be closed so that the user can no longer return to them. But you can go back to your previous activity.
This is useful when your program has a split point. For example, an e-mail application may have an action to view an attachment that needs to start a picture browse activity to display. This activity should be part of the e-mail application task because this is the action that the user triggers in this task. However, when the user leaves this task and then chooses the e-mail app from the main screen, we may want to go back to the viewing session, but not to view the picture attachment, as this is confusing. By setting this flag when the image is started, browsing and other startup activity will be cleared all the next time the user returns to the mail program.
Flag_activity_exclude_from_recents
If set, the new activity is not saved in the list of recently launched activity.
Flag_activity_forward_result
If set, and the intent is used to initiate a new activity from an existing activity, the activity that is the target of the response will be propagated to the new activity. In this way, the new activity can call Setresult (int), and the resulting value will be sent to the activity that is the target of the reply.
Flag_activity_launched_from_history
This flag is generally not set by the application code, and if the activity is initiated from the history (often by pressing the home key), then the system will set it up for you.
Flag_activity_multiple_task
Do not use this flag unless you implement the Application Launcher yourself. Used in conjunction with Flag_activity_new_task, you can disable the behavior of sending a saved task into the foreground. When set, the new task will always start to handle intent, regardless of whether this is already a task that can handle the same thing.
Because the default system does not contain graphics task management functionality, you should not use this flag unless you provide a way for the user to return to a task that has already been started.
If the FLAG_ACTIVITY_NEW_TASK flag is not set, this flag is ignored.
Flag_activity_no_animation
If set in intent and passed to Context.startactivity (), this flag will prevent the system from entering the next activity when the acitivity migration animation is applied. This does not mean that the animation will never run-if another activity does not specify this flag before the display is started, then the animation will be applied. This flag can be used to perform a series of operations, and animations are seen as drivers of higher-level events.
Flag_activity_no_history
If set, the new activity will no longer be retained in the history stack. Once the user has left it, the activity is closed. This can also be done by setting the Nohistory attribute.
Flag_activity_no_user_action
If set, this flag will block the Onuserleavehint () from the front activity callback until the activity is paused as the newly initiated activity enters the foreground.
Typically, an activity can rely on this callback to refer to an activity that is caused by an explicit user action to move out of the background. This callback marks an appropriate point in the activity's life cycle and closes some notification.
If an activity is initiated through a non-user-driven event, such as a call or alarm, this flag should also be passed to context.startactivity, which ensures that the inactive activity does not assume that the user is aware of its notification.
Flag_activity_previous_is_top
If set and this intent are being used to launch a new activity from an existing one, the current activity would not be count Ed as the top activity for deciding whether the new intent should is delivered to the top instead of starting a new one. The previous activity is used as the top, with the assumption being that the current activity would finish itself imme Diately.
Flag_activity_reorder_to_front
If set in intent and passed to Context.startactivity (), this flag will cause the activity that is already running to move to the top of the history stack.
For example, suppose a task consists of four activity: A,b,c,d. If D calls StartActivity () to start activity B, then B moves to the top of the history stack, and now the order becomes a,c,d,b. If the FLAG_ACTIVITY_CLEAR_TOP flag is also set, then this flag will be ignored.
9. Please describe the life cycle of the activity.
A: Activity life cycle methods are: OnCreate (), OnStart (), Onrestart (), Onresume (), OnPause (), OnStop (), ondestory ();
Visible life cycle: from OnStart () until system calls OnStop ()
Foreground life cycle: from Onresume () until system call OnPause ()
10. Activity in the life cycle of the screen rotation
A: When you do not set the activity's android:configchanges, the screen will recall the life cycle, cut across the screen will be executed once, cut the vertical screen will be executed two times; set activity's android:configchanges= " Orientation ", the screen will still recall the life cycle, cut horizontally, vertically, only once, set the activity's android:configchanges=" Orientation|keyboardhidden ", The screen does not recall the individual lifecycles, only the Onconfigurationchanged method is executed
11, how to enable service, how to deactivate service.
The development of the service is relatively simple, as follows:
First step: Inheriting the service class
public class SMSService extends Service {}
Step Two: Configure the service in the node in the Androidmanifest.xml file:
The service cannot run itself and needs to start the service by calling the Context.startservice () or Context.bindservice () method. Both of these methods can start the service, but they are used in different situations. The service is enabled with the StartService () method, the caller is not connected to the service, and the service is still running even if the caller exits. Using the Bindservice () method to enable the service, the caller and the service are bound together, and once the caller exits, the service terminates, and there is a "no need to live at the same time Must Die" feature.
If you plan to start the service with the Context.startservice () method, the system calls the service's OnCreate () method first, and then calls the OnStart () method when the service is not created. If the StartService () method is called before the service has been created, calling the StartService () method multiple times does not cause the service to be created more than once, but results in multiple calls to the OnStart () method. Services started with the StartService () method can only call the Context.stopservice () method to end the service, and the OnDestroy () method is called at the end of the service.
If you plan to start the service with the Context.bindservice () method, the system calls the service's OnCreate () method first, and then calls the Onbind () method when the service is not created. This time the caller and the service are bound together and the caller exits, the system calls the service's Onunbind () method first, and then calls the OnDestroy () method. If the service is already bound before the Bindservice () method is called, multiple calls to the Bindservice () method do not cause the service and bindings to be created multiple times (that is, the OnCreate () and Onbind () methods are not called multiple times). If the caller wants to unbind the service being bound, it can call the Unbindservice () method, and calling the method will also cause the system to invoke the service's Onunbind () –>ondestroy () method.
The service common life cycle callback method is as follows:
OnCreate () This method is called when the service is created, and the method is only called once, and the service is created only once, regardless of how many times the StartService () or Bindservice () method is called.
OnDestroy () This method is called when the service is terminated.
Life cycle methods related to starting services with the Context.startservice () method
OnStart () The method is only invoked when the service is started with the Context.startservice () method. This method is called when the service starts running. The StartService () method is called multiple times although the service is not created more than once, but the OnStart () method is called multiple times.
Life cycle methods related to starting services with the Context.bindservice () method
Onbind () The method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are bound, and the Context.bindservice () method is called multiple times when the caller is tied to the service and does not cause the method to be called more than once.
Onunbind () The method is only invoked when the service is started with the Context.bindservice () method. This method is called when the caller and the service are unbound
12. There are several ways to register a broadcast, what are the pros and cons of these methods? Please talk about the intention of Android to introduce a broadcasting mechanism.
Answer: First write a class to inherit Broadcastreceiver
The first type: declared in the manifest file, adding


Baidu's Android interview summary analysis

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.