Android Common face question (i)

Source: Internet
Author: User
Tags sqlite database

Summed up some of the common Android interview questions, content will continue to increase with continuous learning. The answer is wrong where I hope you can correct me, and hope that we all add more common questions, thank you ^_^

1. Brief description of activity life cycle
2. Simply say what you know about the broadcast recipient
3. How does the view refresh? Brief description of what is double buffering?
4. What is the full name of Aidl? How does it work? What types of data can be processed?
5. How to reference the local language in Java
6. Talk about the IPC (interprocess communication) mechanism of Android
7. What is the NDK?
8. How to publish the SQLite database (xxx.db file) with the apk file

1. Brief description of activity life cycle

1. Activity Visible and focus
When activity starts, it first calls the OnCreate (), OnStart (), Onresume () method, at which time the activity is visible to the user.

2. Activity visible but not focused
The OnPause () method is called when activity changes from visible to dialog occlusion, where activity is visible to the user but does not get the focus

3. Activity is not visible
The OnPause ()//onstop () method is called in turn when the activity changes from visible to being fully covered by other activity or when the home is clicked into the background, and if the system is out of memory during this period, the activity is recycled. The Ondestory () method is also called

4, activity visible, not get focus state –> activity visible, get focus state
The Onresume () method is called when activity resumes from a state that is blocked by dialog, which restores the state that can be clicked

5, activity is not visible, not get focus state –> activity visibility, get focus state
When activity is recovered from being obscured by other activity or in the background state, if it is not reclaimed by the system, the Onrestart (), the OnStart (), and the Onresume () method are called in turn to revert to a state that can interact with the user.

6, activity is not visible state –> activity is recycled –> activity visible, get focus state
When the activity has been obscured by other activity or is in the background, and is reclaimed by the system, the activity is resumed at this time, which is equivalent to reopening an activity, calling the OnCreate (), OnStart ()//onresume () method, This restores the state to which the user can interact (during which onrestoreinstancestate () can be called and the interface is restored).

7, activity visible, get focus state –> activity visible, not get focus state or invisible state
After the OnPause () method executes, the system will stop some CPU-consuming operations, because this time the priority of the program is reduced, it is likely to be retracted by the system, so we should do in this method of data persistence processing. The saved data can be read in Onresume () to help the user revert to the previous state.

8. Activity End
After OnDestroy () is executed, the activity life cycle is over and can be judged by the isfinishing () method. If there is Progress dialog display at this time, we should cancel in OnDestroy (), or the end of the thread, call dialog Cancel method will throw an exception.

2. Simply say what you know about the broadcast recipient

The broadcast recipient (Broadcastreceiver) is used to receive broadcast intent, and the broadcast intent is sent by calling Context.sendbroadcast (), Context.sendorderedbroadcast () To achieve. Typically a broadcast intent can be received by multiple broadcast receivers subscribed to this intent, which is similar to topic message recipients in JMS. The method to implement a broadcast receiver is as follows:
The first step: Inherit Broadcastreceiver and override the OnReceive () method.

publicclass IncomingSMSReceiver extends BroadcastReceiver {     @OverridepublicvoidonReceive(Context context, Intent intent) {     } }

The second step: Subscribe to the specified broadcast intent, there are two ways to subscribe:
The first: Subscribe using code

filternew IntentFilter("android.provider.Telephony.SMS_RECEIVED"newfilter);

The second type: Subscribe to the node in the Androidmanifest.xml file:

  <receiver android:name=".IncomingSMSReceiver">      <intent-filter>          <action android:name="android.provider.Telephony.SMS_RECEIVED"/>     </intent-filter></receiver>

Broadcast type:

Broadcast is divided into two different types: normal broadcasts and ordered broadcast (Ordered broadcasts). The normal broadcast is completely asynchronous and can be received by all receivers at the same time (logically), with a higher efficiency of message delivery, but the disadvantage is that the receiver cannot pass the processing result to the next recipient and cannot terminate the broadcast intent propagation; but ordered broadcasts are in accordance with the priority level declared by the receiver. The receiver receives the broadcast in turn. For example, if the level of a is higher than b,b, then the broadcast is passed first to a, then to B, and finally to C. The precedence level is declared in the android:priority attribute of the intent-filter element, the higher the number of precedence, the greater the value range: 1000 to 1000, and the precedence level can also be set by calling the SetPriority () of the Intentfilter object. The receiver of an orderly broadcast can terminate the broadcast intent, and once the broadcast intent is transmitted, the receiver will not be able to receive the broadcast. In addition, the recipient of an ordered broadcast can pass the data to the next recipient, such as: A is broadcast, it can be stored in its result object, and when broadcast is passed to B, B can get a data from a set of result objects.

Context.sendbroadcast () sends a normal broadcast, and all Subscribers are given the opportunity to obtain and process it.

Context.sendorderedbroadcast () sends an ordered broadcast, and the system executes the receiver sequentially, based on the priority level declared by the receiver, and the preceding receiver has the right to terminate the broadcast

Broadcastreceiver.abortbroadcast (), if the broadcast is terminated by the previous receiver, the subsequent recipient will no longer be able to obtain the broadcast. For ordered broadcasts, the previous receiver can store the data in the result object via the Setresultextras (bundle) method, then pass it to the next recipient, the next recipient through code: Bundle bundle = Getresultextras (True)) You can get the data that was stored in the result object by the previous recipient.

The system receives the text message, the broadcast belongs to the orderly broadcast. If you want to prevent users from receiving text messages, you can set a priority so that your customized recipients get the broadcast first, and then terminate the broadcast so that the user cannot receive the message.

Broadcast recipient's response:
in Android, each time a broadcast message arrives, a Broadcastreceiver instance is created and the OnReceive () method is executed, and after the OnReceive () method finishes, Broadcastreceiver Instances will be destroyed. When the OnReceive () method is not completed within 10 seconds, Android will consider the program unresponsive. Therefore, in the broadcastreceiver can not do some more time-consuming operation, the side will pop up the ANR (Application No Response) dialog box. If you need to complete a more time-consuming task, you should do so by sending intent to the service. It is not possible to use a child thread here because the Broadcastreceiver life cycle is short, and the child thread may not have finished broadcastreceiver before it ends. Once the broadcastreceiver is over, the broadcastreceiver process can easily be killed when the system needs memory because it belongs to the empty process (the process without any active components). If its host process is killed, then the working child thread will be killed. So using sub-threading to solve is unreliable.

publicclass IncomingSMSReceiver extends BroadcastReceiver {     @Override     publicvoidonReceive(Context context, Intent intent) {             //发送Intent启动服务,由服务来完成比较耗时的操作            new Intent(context, XxxService.class);             context.startService(service);     } }

Popular Broadcast Intent:

In addition to the SMS arrival broadcast intent,android There are many broadcast intent, such as: Boot start, battery power changes, time has changed, such as broadcast intent.

Receive boot start broadcast intent, subscribe to this intent in the node in the Androidmanifest.xml file:

<receiver android:name=".IncomingSMSReceiver">     <intent-filter>          <action android:name="android.intent.action.BOOT_COMPLETED"/>     </intent-filter></receiver>

And to make a permission declaration:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

Receive battery change broadcast intent, subscribe to this intent in a node in the Androidmanifest.xml file:

<receiver android:name=".IncomingSMSReceiver">     <intent-filter>          <action android:name="android.intent.action.BATTERY_CHANGED"/>     </intent-filter></receiver>

Life cycle of broadcast receivers and precautions for use

1. The life cycle of the broadcast receiver is very short, created when the broadcast is received, and destroyed after the OnReceive () method finishes

2, the broadcast receiver do not do some time-consuming work, otherwise it will pop up application No Response error dialog box, time-consuming longer work is best placed in the service to complete

3, it is best not to create sub-threads in the broadcast receiver to do time-consuming work, because the broadcast receiver is destroyed after the process becomes an empty process, it is easy to be killed by the system

3.view How to refresh? Brief description of what is double buffering?

There are two ways to refresh view in Android, one is invalidate () and the other is Postinvalidate (), which is used in the UI thread itself, while the latter is used in a non-UI thread.

Screen flicker is a common problem with graphics programming. A complex drawing operation can cause the rendered image to blink or have other unacceptable appearance. The use of double buffering solves these problems. Double buffering uses memory buffers to resolve flicker problems caused by multi-draw operations. When double buffering is used, all drawing operations are done in the memory buffer first, instead of drawing directly on the screen. When all the drawing operations are complete, copy the image of the memory buffer complete directly to the screen. Because only one graphics operation is performed on the screen, the problem of image flicker caused by complex drawing operations is eliminated.

With dual buffering in Android, you can use a background canvas backcanvas, where all of the drawing operations are done first. Wait until the picture is ready, and then copy the Backcanvas to
The canvas that is associated with the screen is as follows:

Bitmap bitmapBase = new Bitmap()Canvas backcanvas = new Canvas(bitmapBase)backcanvas.draw()...//画图Canvas c = lockCanvas(null);c.drawbitmap(bitmapBase);//把已经画好的图像输出到屏幕上unlock(c)....

4. What is the full name of Aidl? How does it work? What types of data can be processed?

The full name is: Android Interface Define Language

In Android, each application can have its own process. When writing UI applications, you often need to use the
Service. How do you pass objects in different processes? Obviously, cross-process memory sharing is not allowed in Java. Therefore, the object can only be split into simple forms that the operating system can understand to achieve the purpose of cross-boundary object access.

In Java EE, an RMI is used to pass objects by serialization. In Android, the aidl is used.

Theoretically aidl can pass bundles, actually doing it is rather troublesome.

Aidl (AndRoid Interface Description Language) is an excuse to describe the language; The compiler can generate a piece of code through the Aidl file, which achieves the purpose of two process internal communication processes through a pre-defined interface. If you need to access an object in another Service in one activity, you need to first turn the object into a aidl-recognizable parameter (possibly multiple parameters), then use Aidl to pass these parameters, and at the receiving end of the message, use these parameters to assemble the objects that you need.

Aidl's IPC mechanism is similar to COM or CORBA, and is interface-based, but it is lightweight. It uses a proxy class to pass values between the client and the implementation layer. If you want to use aidl, you need to complete 2 things: 1. Introduction of AIDL related classes; 2. Call the class generated by the Aidl.

How to create a aidl:

The AIDL syntax is simple and can be used to declare an interface with one or more methods, or to pass parameters and return values.
These parameters and return values are not of any type because of the need for remote calls. Here are some of the data types supported by Aidl:
1. Simple Java programming language types (Int,boolean, etc.) that do not require import declarations;
2, String, charsequence do not need special declaration;
3, List, Map and Parcelables types, the data members contained within these types can only be simple data types, String and other supported types.

5. How to reference the local language in Java

You can use the JNI (Java Native Interface Java local interface) interface

6. Talk about the IPC (interprocess communication) mechanism of Android

IPC is the abbreviation for internal process communication and is a resource for sharing named pipes. The IPC mechanism in Android is designed to
So that the activity and service can interact at any time, so in Android, this mechanism is only applicable to activity and service communication, similar to the remote method call, similar to the C/S mode of access. Define the IPC interface by defining the Aidl interface file. The Servier end implements the IPC interface, and the Client side invokes the IPC interface local agent.

7. What is the NDK?

The NDK is a collection of columns, and the NDK provides a range of tools to help developers quickly develop a dynamic library of C + + and automatically make so and Java apps into apk packages.

The NDK integrates the cross compiler and provides the corresponding MK file and isolate the CPU, platform, etc., developers can simply modify the Mk file to create a so.

8. How to publish the SQLite database (xxx.db file) with the APK file

You can copy the Xxx.db file to the Res AW directory in the Eclipse Android project. All files in the Res AW directory are not compressed so that the files in that directory can be extracted directly. The xxx.db file can be copied to the Res AW directory.

Android Common face question (i)

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.