Android interview question sorting and explanation (2)

Source: Internet
Author: User

1. What are the main differences between Dalvik and standard Java virtual machines? One of the primary differences between Dalvik and the standard Java Virtual Machine (JVM) Is that Dalvik is based on registers while JVM is based on stacks.
Another major difference between Dalvik and Java is the runtime environment-Dalvik is optimized to allow running instances of multiple virtual machines in limited memory at the same time, each Dalvik application is executed as an independent Linux Process.
(1) the virtual machine is small and the space used is small;
(2) Dalvik does not have the JIT compiler;
(3) The constant pool has been changed to a 32-bit index only to simplify the interpreter;
(4) it uses its own bytecode instead of Java bytecode.
That is to say, every application runs on an independent dalvik virtual machine on the Android platform, and every dalvik virtual machine is the next process in LINUX. Therefore, the process of the application, the process of the dalvik Virtual Machine and the process of LINUX are all meanings.
2. What is the role of onSaveInstanceState? To ensure the correctness of the program, where do I write the code for the persistent layer operation? OnSaveInstanceState is called when the Activity is accidentally blocked (the difference is that it is different from the user's manual exit). For example, it is called only when the user suddenly calls the phone or clicks the Home key, the application interface may be killed in the background or not, so we can save some interface values in this method. If the Activity is killed, these values are persisted to the hard disk of the mobile phone. When we enter it again, we can call onRestoreInstanceState to restore the interface. Since both the onSaveInstanceState and onRestoreInstanceState methods are not necessarily called by the system, we generally perform data persistence operations in the onPause () method.
3. How to refresh the view? What is double buffering? There are two methods to update the view in android: invalidate and postInvalidate. The former is used in the UI thread itself, and the latter is used in non-UI threads.

Flashing is a common problem in graphic programming. Complex rendering may cause the rendered image to flash or have other unacceptable appearances. The use of double buffering solves these problems. The dual-buffer uses the memory buffer to solve the flickering problem caused by multiple painting operations. When dual buffering is used, all the painting operations are completed in the memory buffer, instead of drawing directly on the screen. After all the painting operations are completed, copy the images completed in the memory buffer zone to the screen. Because only one graphic operation is performed on the screen, image flashing caused by complex painting is eliminated.
To implement dual Buffering in android, you can use a background canvas backcanvas to perform all the painting operations first. After the picture is finished, copy the backcanvas
The canvas associated with the screen is as follows:
Bitmap bitmapBase = new Bitmap ()
Canvas backcanvas = new Canvas (bitmapBase)
Backcanvas. draw ()... // draw
Canvas c = lockCanvas (null );
C. drawbitmap (bitmapBase); // output the drawn image to the screen.
Unlock (c )....

4. what do you know about the broadcast receiver? BroadcastReceiver is used to receive the broadcast Intent. The broadcast Intent is sent by calling the Context. sendBroadcast (), Context. sendOrderedBroadcast. Generally, a broadcast Intent can be received by multiple broadcast recipients subscribed to this Intent. This feature is similar to the Topic Message Receiver in JMS. The method to implement a broadcast receiver is as follows:
Step 1: Inherit BroadcastReceiver and override the onReceive () method.
Public class IncomingSMSReceiver extends BroadcastReceiver {
@ Override public void onReceive (Context context, Intent intent ){
}
}
Step 2: subscribe to the broadcast Intent you are interested in. There are two subscription methods:
First: Subscribe using code
IntentFilter filter = new IntentFilter ("android. provider. Telephony. SMS_RECEIVED ");
IncomingSMSReceiver extends ER = new IncomingSMSReceiver ();
RegisterReceiver (receiver, filter );
Type 2: subscribe to the node in the AndroidManifest. xml file:







Broadcast type:

Broadcast is divided into two different types: Normal broadcast (Normal broadcasts) and Ordered broadcast (Ordered broadcasts )". Normal broadcast is completely asynchronous and can be received by all receivers at the same time (logically). The efficiency of message transmission is relatively high, but the disadvantage is: the receiver cannot pass the processing result to the next receiver, and cannot terminate the propagation of the broadcast Intent. However, the ordered broadcast is based on the priority level stated by the receiver, and the receiver receives the broadcast in sequence. For example, if the level of A is higher than that of B and the level of B is higher than that of C, broadcast is first transmitted to A, then to B, and finally to C. Priority Level Declaration is in the android: priority attribute of intent-filter element. The higher the number, the higher the priority level. value range:-1000 to 1000. You can also call setPriority () of the IntentFilter object at the priority level (). The receiver of an ordered broadcast can terminate the spread of the broadcast Intent. Once the spread of the broadcast Intent is terminated, the subsequent receiver cannot receive the broadcast. In addition, the receiver of ordered broadcast can pass the data to the next receiver. For example, after A gets the broadcast, it can store the data in its result object. When broadcast is sent to B, B can obtain the data stored by A from the result object of.

Context. sendBroadcast ()
A normal broadcast is sent, and all subscribers have the opportunity to obtain and process it.
Context. sendOrderedBroadcast ()
An ordered broadcast is sent. The system executes receivers one by one based on the priority level stated by the receiver. The previous receiver has the right to terminate the broadcast (BroadcastReceiver. abortBroadcast (), if the broadcast is terminated by the previous receiver, the subsequent receiver will no longer be able to obtain the broadcast. For ordered broadcast, the previous receiver can store the data in the result object through the setResultExtras (Bundle) method, and then pass it to the next receiver. The next receiver uses the code: Bundle bundle = getResultExtras (true )) you can obtain the data stored by the previous receiver in the result object.


When the system receives a text message, the broadcast is an ordered broadcast. If you want to prevent the user from receiving text messages, you can set the priority so that the custom receiver can first obtain the broadcast and then terminate the broadcast so that the user cannot receive the text message.

Response of the broadcast receiver:
In Android, each time a broadcast message arrives, A BroadcastReceiver instance is created and the onReceive () method is executed. After the onReceive () method is executed, the BroadcastReceiver instance is destroyed. When the onReceive () method is not completed within 10 seconds, Android considers the program to be unresponsive. Therefore, you cannot perform some time-consuming operations in BroadcastReceiver. the ANR (Application No Response) dialog box is displayed on the other side. If you need to complete a time-consuming task, you should send Intent to the Service, which is done by the Service. Subthreads cannot be used here, because the life cycle of BroadcastReceiver is very short, and the subthread may end before it ends. Once BroadcastReceiver ends, the process where BroadcastReceiver is located is easily killed first when the system requires memory because it is a blank process (process without any active components ). If its host process is killed, the working sub-thread is also killed. Therefore, it is unreliable to use sub-threads.

Public class IncomingSMSReceiver extends BroadcastReceiver {
@ Override

Public void onReceive (Context context, Intent intent ){
// Send Intent to start the service. The service performs time-consuming operations.
Intent service = new Intent (context, XxxService. class );
Context. startService (service );
}
}

Common broadcast Intent:

In addition to the arrival of SMS broadcast Intent, Android also has many broadcast Intent types, such as startup, battery power change, and time change.
Receive the battery change broadcast Intent and subscribe to this Intent in the node in the AndroidManifest. xml file:






Receive the boot broadcast Intent and subscribe to this Intent in the node in the AndroidManifest. xml file:





And you need to declare the permission:


Broadcast receiver Lifecycle

The life cycle of the broadcast receiver is very short. It is created when the broadcast is received and destroyed after the onReceive () method ends.
Do not do some time-consuming work in the broadcast receiver; otherwise, the Application No Response error dialog box will pop up.
It is best not to create a sub-thread in the broadcast receiver for time-consuming work, because after the broadcast receiver is destroyed, the process becomes an empty process and is easily killed by the system.
It is best to put the time-consuming work in the service for completion.

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.