Common Android test questions and Answers (detailed collation)

Source: Internet
Author: User
Tags sqlite database

Common Android test questions and Answers (detailed collation) 

1. Please describe the activity life cycle.

Answer: as shown in. There are seven periodic functions, in order: OnCreate (), OnStart (), Onrestart (), Onresume (), OnPause (), OnStop (), OnDestroy ().

OnCreate (): Called when an activity is created, set in the method, and provides access to any previously stored state in the form of bundles.

OnStart (): Called when activity becomes visible to the user on the screen.

Onresume (): Called when activity starts interacting with the user (whether it is starting or restarting an activity, the method is always called.)

OnPause (): When activity is paused or retracted CPU and other resources are called, the method user protects the active state and is also protected on site.

OnStop (): Called when activity is stopped and turned into an invisible phase and subsequent life-cycle events.

Onrestart (): Called when activity is restarted. The activity is still in the stack instead of starting a new activity.

1, the Complete life cycle: that is, from the occurrence of an activity to disappear, the corresponding cycle method is from OnCreate () to OnDestroy ().

2. Visible life cycle: When the activity is in a state that can be seen by the user, but does not necessarily interact with the user, it will be executed multiple times from OnStart () to OnStop ().

3. The foreground life cycle: When activity is at the top of the activity stack and can interact with other users, it executes multiple times from Onresume () to OnPause ().

2. What are some of the methods that must be performed when jumping between two activity.

A: Jumping between two activity is bound to be performed in the following ways.

OnCreate ()//is called at the beginning of the activity life cycle.

Onrestoreinstancestate ()//is used to restore the UI state.

Onrestart ()//called when activity restarts.

OnStart ()//Called when activity is about to be visible to the user.

Onresume ()//When activity interacts with the user, the interface is drawn.

Onsaveinstancestate ()//is called when the top of the stack is moved out of the left UI state.

OnPause ()//suspend active activity, commit changes to persistent data, stop animations or other things that occupy GPU resources, because the next activity will not resume before this method returns, so the code for this method executes faster.

Called when OnStop ()//activity is no longer visible.

OnDestroy ()//activity The last method that was called when the stack was destroyed.

3. The life cycle of the activity when the screen is switched.

For:

1, do not set the activity of the android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed two times.

2, set the activity android:configchanges= "orientation", the screen will recall the various life cycle, cut horizontal, vertical screen will only be executed once.

3, set the activity android:configchanges= "Orientation|keyboardhidden", the screen will not recall the various life cycle, will only execute Onconfiguration method

4. How to set an activity to the window's style.

A: The first method, in the Styles.xml file, you can create a new style like the dialog as follows.

<style name= "theme.floatactivity" parent= "Android:style/theme.dialog" > </style>.

The second method, in Androidmanifest.xml, adds the following properties to the activity that needs to be displayed as a window: android:theme= "@style/theme.floatactivity". You can also add directly the Android:theme property of the activity that needs to be shown as Dialog style to Android:theme= "@ android:style/theme.dialog".

5. How do I pass data between two activity?

A: You can use extra to pass stored data in a intent object.

In the intent object request, use the Putextra (name of the key-value pair, the value of the key-value pair), and the request data in the intent in another activity:

Intent Intent = Getintent ();

String value = Intent.getstringextra ("testintent");

6. How do I start a service when an activity is started?

A: First define the good one service and then connect within the activity's oncreate and Bindservice or StartService directly.

7. How does the activity bind to the service, and how to initiate its own service in the activity?

For:

1. Activity can be bound to benefit from the Serviece interface. To support the binding of the service, implement the Onbind method.

2, service and activity connection can be realized by serviceconnection. A new serviceconnection is required to reproduce the onserviceconnected and onservicedisconnected methods, and once the connection is established, a reference to the service instance can be obtained.

3. Execute the binding, call the Bindservice method, pass in a intent (display or implicit) that chooses the service to bind, and an instance of serviceconnection that you have implemented

8. What is the service and describes its life cycle. What are the service startup methods, what is the difference, and how do I deactivate the service?

A: Android service is code that runs in the background, cannot interact with the user, can run in its own process, or can run in the context of other application processes. It needs to be called through an activity or context object. The service has two boot methods, Context.startservice () and Context.bindservice () respectively. If the service performs a time-consuming operation, it needs to start a new thread to execute.

Android Service only inherits OnCreate (), OnStart (), OnDestroy () Three methods, when we first start the service, we call OnCreate (), OnStart () the two methods, When the service is stopped, the OnDestroy () method is executed. If the service is already started, when we start the service again, the OnCreate () method is no longer executed, but the OnStart () method is executed directly.

9. When do I use the service?

A: For example, when playing multimedia, the user initiates other activity, this time the program will continue to play in the background, such as detecting the changes in the file on the SD card, or in the background to record your geographic information location changes and so on.

10. Please describe the Intent and Intent Filter.

A: Intent is translated as "intent" on Android and is a means of activating each of the three basic components of the application-activity,service and broadcast receiver. Called when the intent name is called using ComponentName, which is the full name of the class. This approach is typically used for internal invocation of the application, because you don't necessarily know the full name of the class that someone else wrote. The intent filter refers to the intention of filtering, not appearing in the code, but in the form of an Android manifest file, in <intent-filter>. (one exception is broadcast receiver's intent
Filter is dynamically set using Context.registerreceiver (), where intent filter is also created in the code)

A intent has fields such as action,data,category. An implicit intent in order to be able to be received by a intent filter, must pass 3 tests, a intent in order to be received by a component, it must pass one of its intent filter.

Intent. What types of data can be passed when data is passed?

A: There are two commonly used methods for transmitting data between intent: 1, extra 2, and.

Extra can put data in Intent.putextra. The newly initiated activity can use Intent.getextras to take out bundles, and then uses functions such as bundles.getlong,getint,getboolean,getstring to get the values that are put in.

Data is the transport URL. URLs can refer to network addresses such as http,ftp that we are familiar with, or content to point to resources provided by ContentProvider. Intent.setdata can be put into data, Intent.getdata can take out data.

12. What is the relationship between Activity,intent,service and speaking?

A: An activity is usually a separate screen, and each activity is implemented as a separate class that inherits from the activity base class. The activity class displays the user interface that is composed of the view controls and responds to the events of the views control.

The intent call is used to switch between screens. Intent describes what the app wants to do. Two of the most important parts of the intent data structure are the actions and actions that correspond to an action data.

The service is code that runs in the background, cannot interact with the user, can run in its own process, or can run in the context of other application processes. Requires an activity or other context object to invoke.

Activity Jump activity,activity Start Service,service open activity all need intent to indicate intent, as well as pass parameters, intent is the bearer of the signal transmission between these components.

13. Please describe the broadcastreceiver.

Answer: Broadcast receiver is used to receive and process broadcast notifications (broadcast announcements). Most of the broadcasts are initiated by the system, such as geographical change, low power, incoming SMS, etc. The program can also play a broadcast. The program can have any number of broadcast receivers to respond to the notifications it feels important. Broadcast receiver can notify users in a variety of ways: Start activity, use Notificationmanager, turn on background lights, vibrate devices, play sound, etc., most typically in the status bar display an icon, This allows the user to click on it to open the notification content. Usually one of our applications or the system itself in certain events (low battery, call SMS) to temporarily broadcast a intent out, we use to register a broadcast
Receiver to listen for these intent and get the data in the intent.

14. How to register and use broadcast receiver in manifest and code.

Answer: Register in Android Manifest

<receiver android:name = "Receiver1" >

<intent-filter>

< action in the!----and intent--->

<actionandroid:name= "Com.forrest.action.mybroadcast"/>

</intent-filter>

</receiver>

Registering in code

1, Intentfilter filter = new Intentfilter ("Com.forrest.action.mybroadcast");//and the intent action corresponding to the broadcast;

2, Mybroadcastreceiver br= new Mybroadcastreceiver ();

3, Registerreceiver (BR, filter);

15. Please describe how the next contentprovider is to achieve data sharing.

A: A program can completely expose its own data by implementing a content provider abstraction interface, and content provider exposes its own data in a way similar to a table in a database. Content provider stores and retrieves data, which allows all applications to access, which is the only way to share data between applications.

To make your application's data public, there are 2 ways to create a data own content provider or add your data to an existing content provider, provided that there is the same data type and that there is write content Provider permissions, Android provides content resolverr, and external programs can access the content provider data provided through the content resolver interface.

16. Please describe how the data is stored in Android.

A: Android provides a way to store data in 5, respectively, in the following ways

1, using shared preferences storage data, used to store data in key-value,pairs format, it is a lightweight key-value storage mechanism, can only store basic data types.

2, the use of files to store data, through FileInputStream and FileOutputStream to operate the file. In Android, files are private to an application, and one application cannot read or write files from other applications.

3, using the SQLite database to store data, Android provides a standard database, support SQL statements.

4. Using content provider to store data is a bridge between all applications for data storage and retrieval, and it is the role of data sharing between applications. It is a special type of storage data that provides a standard set of interfaces for obtaining data and manipulating data. The system also provides several common content Provider such as audio, video, image, and personal information. If you want to expose your own private data, you can create your own content provider class, or add the data to the content provider for sharing when you have permission to control writes to that data. External access accesses and operates these exposed data through the content resolver.

5. Using the network to store data

17. Please describe the five types of layouts commonly used in Android.

A: The most commonly used layout methods are absolute layout, Relative layout, Linear layout, framelayout, Tablelayout. Linear layout and relative layout are the most common ways in which they can be laid out in an XML configuration file or in code.

1, Frame layout is the simplest way to lay out the controls can only be listed to the upper left corner, the control will overlap, can not be complex layout.

2, Linear layout can be set by the Orientation property of the linear arrangement of the direction is vertical or vertical, each row or column has only one element, can be complex layout.

3. Absolute layout allows child elements to specify accurate X-and y-coordinate values and display them on the screen. Absolute layout does not have a page border, allowing elements to overlap each other. It is an absolute coordinate, so it is not advocated in practice.

4. Relative layout allows child elements to establish their position relative to other elements or parent elements (by ID). As a result, you can arrange two elements in the form of Right-aligned, or up or down, or centered on the screen. Elements are arranged sequentially, so if the first element is in the center of the screen, then the other elements relative to the element are arranged in the relative position of the center of the screen. This is relative to the absolute layout, the use of relative coordinates, so in practice is more commonly used.

5. Table layout assigns the position of the child element to the row or column. A table layout consists of a number of table row, and each table row defines a row. The Table layout container does not display the row, column, or cell edge boxes. Each row has 0 or more cells, similar to the table in HTML. It is often used in practice.

18. What is the difference between padding and margin in the UI?

A: Padding is the margin of the control's content relative to the edge of the control, and margin is the margin of the control's edge relative to the other control. As shown in the following:

Some limitations of Android itself, such as the APK packet size limit, the time limit when reading large files.

A: APK packet size limit is not good to say, some apk is 100M, or can be installed on the phone. The average apk size is around 5~10m. The time to read large files should be within the main thread, with a time limit of about 5 seconds.

How does the ListView improve its efficiency?

A: 1, use page load, do not load all the data at once.

2, Reuse Convertview. In Getitemview, determine if the Converview is empty, and if it is not empty, it can be reused.

3. Load images asynchronously. If the item contains Webimage, it is best to load asynchronously.

4. When sliding quickly, the picture is not displayed. When the Quick Slide list (scroll_state_fling), the picture in item or the view that needs to consume resources, can not be displayed, but in the other two states (Scroll_state_idle and Scroll_state_touch_ SCROLL), the view is displayed

What is the difference between Android programs and Java programs?

Difference

Java programs

Android Program

Execution entry point

Main

Intent-filter in the Android mainfest.xml configuration file

Ui

Frame

Activity

is based on the configuration file

Whether

Is

is component-based

Whether

It's Activity,service,broadcast receiver,content Provider.

22. Talk about the understanding of the Android NDK.

A: The Android NDK is a set of tools that allows Android app developers to embed native machine code compiled from C, c + + source code into their respective application packages.

1. The NDK is a collection of tools.

The NDK provides a range of tools to help developers quickly develop C (or C + +) dynamic libraries and automatically package so and Java applications together as APK. These tools are great for developers to help with.

The NDK integrates the cross-editor and provides the corresponding MK file isolation CPU, platform, API and other differences, developers simply need to modify the Mk file ("Those files need to compile", "compile feature requirements", etc.), you can create a so. The NDK can automatically package so and Java applications together, greatly reducing the developer's packaging effort.

2. The NDK provides a stable, functionally limited API header file declaration. These APIs support a very limited number of features, including: C standard library (LIBC), Standard math Library (LIBM), compression library (LIBZ), log Library (Liblog).

23. Talk about the advantages and disadvantages of Android.

A: The advantages are as follows:

1, Android is open source, the market share is big.

2, Android can have a wealth of hardware choices.

3, Android is a Linux platform based open-source operating system, thus avoiding the patent barrier, is a completely free only mobile platform.

4. Because Android is developed by Google, it can be seamlessly combined with Google Apps.

The disadvantages are as follows:

1, security and privacy. Mobile phones are closely linked to the Internet, and personal privacy is hard to keep.

What happens to a memory leak in a GC in Android system?

A: The main reason for memory leaks is that you previously requested memory space and forgot to release it. If a reference to a useless object exists in the program, those objects will reside in memory and consume memory because the garbage collector GC cannot verify that the objects are no longer needed. If there is a reference to the object, the object is defined as a "valid activity" and is not freed. To make sure that the object's memory will be recycled, we need to confirm that the object is no longer in use. The typical practice is to either set the object data member to null or remove the object from the collection. A memory leak is caused when the following conditions occur:

1. The cursor of the database is not closed.

2. When constructing adapter, the cache Contentview is not used.

3, when the bitmap object is not used, use recycle () to free memory.

4. The life cycle of the object in activity is greater than activity.

Debugging method: Ddms==>heapsize==>dataobject==>[totalsize]

How the view in the Android UI is refreshed.

A: There are many ways to update view in Android, so you need to distinguish between different applications when you use them. To distinguish between: multithreading and double buffering.

1. Do not use multithreading and double buffering

This is the simplest scenario, and it is generally only desirable for the view to redraw the UI when changes occur. You only need to explicitly invoke the Invalidate () method in the View object in the activity. The OnDraw () method of the view is automatically called.

2. Use multithreading and do not use double buffering

In this case, the new thread needs to be opened, and the newly opened thread will not have access to the View object. Forced access will be error: android.view.viewroot$ calledfromwrongthreadexception:only theoriginal thread that created a view hierarchy can touch its views.

At this point you need to create a subclass that inherits the Android.os.handler and override the Handlemessage method. Android.os.Handle is capable of sending and processing messages, you need to emit a message in the activity that updates the UI, and then your handler (you can use anonymous inner classes) to process the message (because the anonymous inner class can access the parent class variable, You can directly invoke the invalidate () method in the View object. That is, you create and send a message in a new thread, and then capture and process the message in the main threads.

3. Use multithreading and double buffering

Android's Surfaceview is a subclass of view, and she also implements double buffering. You can define a subclass of her and implement the Surfaceholder.callback interface. Because of the Surfaceholder.callback interface, the new thread will not android.os.Handler help. The Lockcanvas () method in Surfaceholder can lock the canvas and call Unlockcanvasand post unlock after the new image is drawn.

Common Android test questions and Answers (detailed collation)

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.