Android interview questions (3) and android interview questions

Source: Internet
Author: User

Android interview questions (3) and android interview questions

1. Describe the lifecycle of an Activity.

Activity lifecycle methods include:

OnCreate (), onStart (), onReStart (), onResume (), onPause (), onStop (), on

Destory ();

Visible life cycle: from onStart () to onStop () called by the System ()

Foreground lifecycle: From onResume () to onPause () called by the System ()

 

2. How to enable and disable the Service.

Service development is relatively simple, as follows:

Step 1: Inherit the Service class

Public class SMSService extends Service {}

Step 2: add

Row configuration: <service android: name = ". SMSService"/>

The service cannot run on its own. You must call Context. startService () or

Start the service using the Context. bindService () method. Both methods can start the Service,

They are used differently. Use startService () to enable services, callers and services

The service is still running even if the caller exits. BindService () method

When the service is enabled, the caller and the service are bound together. Once the caller exits, the service is terminated,

It has the characteristics of "not seeking to live at the same time, but having to die at the same time.

If you plan to start the service using the Context. startService () method, when the service is not created,

The system first calls the onCreate () method of the service and then calls the onStart () method. If you call

The service has been created before the startService () method. The startService () method is not called multiple times.

The onStart () method is called multiple times. Use startService ()

Only the Context. stopService () method can be called to end the service.

The onDestroy () method is called.

If you plan to start the service using the Context. bindService () method, when the service is not created,

The system first calls the onCreate () method of the service and then calls the onBind () method. Call

The user and the service are bound together. When the caller exits, the system will first call the onUnbind () of the service ()

Method, and then call the onDestroy () method. If the service already exists before the bindService () method is called

The bindService () method is called multiple times and does not cause multiple service creation and binding (that is

The onCreate () and onBind () methods are not called multiple times ). If the caller wants

You can call the unbindService () method to unbind a bound service.

The onUnbind () --> onDestroy () method of the system call service.

Common lifecycle callback methods for services are as follows:

OnCreate () This method is called only once when the service is created.

The number of startService () or bindService () methods, the service is created only once.

OnDestroy () is called when the service is terminated.

Life cycle method related to starting a service using Context. startService ()

OnStart () calls back this method only when the Context. startService () method is used to start the service.

This method is called when the service starts running. The startService () method is called multiple times, although not multiple times.

Create a service, but the onStart () method is called multiple times.

The lifecycle method related to starting a service using the Context. bindService () method

OnBind () calls back this method only when the Context. bindService () method is used to start the service.

This method is called when the caller is bound to the service. When the caller is bound to the service, it is called multiple times.

The Context. bindService () method does not cause this method to be called multiple times.

OnUnbind () is called back only when the Context. bindService () method is used to start the service.

Method. This method is called when the caller and the service are unbound.

 

3. the scope is public, private, and protected.

The differences are as follows:
Scope: current class, same package, Child class, other package
Public √
Protected √ ×
Friendly √ ××
Private √ ×××

 

4. Can Anonymous Inner Class (Anonymous internal Class) be extends (inherited) other classes or implements (implemented) interface (interface)

An anonymous internal class is an internal class without a name. It cannot be extends (inherited) other classes, but an internal class can be used as an interface and implemented by another internal class

 

5. String s = new String ("xyz"); several String objects are created.

Two, one character object, one character object references the object

 

6. Explain the relationship among Message, Handler, MessageQueue, and logoff in the single-thread model.

In short, Handler gets the logoff object in the current thread, which is used

Extract the Message from MessageQueue that stores the Message, and then perform the Handler operation.

Message distribution and processing.

Message Queue: used to store messages published by Handler.

In a thread that creates it, you can get the message of the current thread through loaning. myQueue ()

Queue Handler: You can publish or process a message or operate a Runnable.

The Handler publishes a message. The message will only be sent to the Message Queue associated with it, and can only be processed.

Messages in this message queue

Logoff: a bridge between Handler and message queue. The program component first uses Handler

The message is sent to the logoff, and The logoff puts the message into the queue. Logoff also

Messages are broadcast to all

Handler: Handler receives the message and calls handleMessage for processing.

Message: the type of the Message. You can obtain the ticket in the handleMessage method in the Handler class.

Messages are processed.

In the single-thread model, Android designs a Message for thread communication problems.

Queue (Message Queue), through which threads can combine Handler and

Logoff component for information exchange

 

7. Briefly explain activity, intent, intentfilter, service, Broadcase, BroadcaseReceiver

An activity presents a visual user interface that users can operate on; a service

It does not contain visible user interfaces, but runs in the background and can be bound to an activity.

Binds exposed interfaces and communicates with them. A broadcast receiver is a widely accepted

For component that broadcasts messages and responds, broadcast aggreger has no interface.

Intent is an Intent object that stores the message content. For activity and service

It specifies the requested operation name and the URI of the data to be operated. The Intent object can explicitly

Specify a target component. In this case, android will find this

Component (based on the declaration in the manifest file) and activates it. However, if a target is not

Android must find the optimal component that responds to the intent. It is through

The Intent object is compared with the intent filter of the target to complete this task.

The intent filter of component tells android that this component can process

Intent. Intent filter is also declared in the manifest file.

 

8. Let's talk about the principles of the mvc model. It is used in android. android officially recommends that the mvc model be used for application development. What is mvc?

Mvc is short for model, view, and controller. mvc consists of three parts:

Model object: it is the main part of the application, and all business logic should

Written in this layer.

View object: A part of the application that generates a user interface. Also in the whole

The only layer that users can see in the mvc Architecture receives user input and displays processing results.

Controller object: controls the display of user interface data and

Updates the status of the model object. A more important navigation function of the controller responds to users.

To the m layer for processing. Android encourages weak coupling and Component Reuse. In android, mvc is embodied as follows:

1) view layer (view): it generally uses xml files for interface description, which can be easily introduced during use. Of course, if you know more about android, yes

You can also use javascript + html as the view layer in android,

Of course, communication between java and javascript is required. Fortunately, android provides

Communication between them is very convenient.

2) control layer (controller): the control layer of android usually has many responsibilities.

This statement implies that you should not write code in acitivity.

Activity delivery model business logic layer processing, another reason for doing so is in android

The response time of the acitivity is 5 s. If the time-consuming operation is put here, the program will be easily returned.

Remove.

3) model layer: database operations and network operations should all be performed on the model

Of course, operations such as business computing must also be processed in this layer.

 

9. What is ANR and how to avoid it?

ANR: Application Not Responding. In Android, the activity manager and window

The two system services of the port manager are responsible for monitoring the response of applications.

The application fails to respond. If BroadcastReceiver is not executed within 10 seconds

The current application has no response dialog box, which is ANR.

Avoidance Method: the Activity should be in its key lifecycle methods (such as onCreate () and

Create as few operations as possible in onResume. Potential time-consuming operations, such as network or data

Data library operations, or high-time computing, such as changing the bitmap size, should be in the Child thread (or asynchronous

Method. The main thread should provide a Handler for the sub-thread so that it can be submitted upon completion.

To the main thread.

 

10. What causes ForceClose? How to avoid it? Can I capture exceptions that cause them?

Program exception, such as nullpointer.

Avoid: the logic is consistent and the thinking is meticulous during programming. Exception can be caught and can be seen in logcat

Common Information

 

11. What is the error? Short s1 = 1; s1 + = 1; what is the error?

Short s1 = 1; s1 = s1 + 1; (s1 + 1 is an int type and requires forced conversion) short s1 = 1; s1 + = 1; (can be compiled correctly)

 

12. Differences between Overload and Override. Can the Overloaded method change the type of the returned value?

Overriding and Overloading are different manifestations of Java polymorphism. Overriding is a manifestation of the polymorphism between the parent class and the Child class, and Overloading is a manifestation of the polymorphism in a class. If a subclass defines a method with the same name and parameter as its parent class, we say this method is overwritten ). When a subclass object uses this method, the definition in the subclass is called. For it, the definition in the parent class is "blocked. If multiple methods with the same name are defined in a class, they may have different numbers of parameters or different parameter types, it is called Overloading ). The Overloaded method can change the type of the returned value.

 

13. The elements in the Set cannot be repeated. How can we identify whether the elements are repeated or not? Is = or equals () used ()? What are their differences?

The elements in the Set cannot be repeated, so the iterator () method is used to identify whether the elements are repeated or not. Equals () is used to determine whether two sets are equal

The equals () and = Methods Determine whether the reference value points to the same object equals () is overwritten in the class, so that when the content and type of the two separated objects match, the true value is returned.

 

14. What are the differences between error and exception?

Error indicates that recovery is not a serious problem that is impossible but difficult. For example, memory overflow. It is impossible to expect the program to handle such a situation

Exception indicates a design or implementation problem. That is to say, it indicates that if the program runs normally, it will never happen.

 

15. introduce how ContentProvider achieves data sharing.

A program can implement an abstract interface of the Content provider to extract its own data.

Completely exposed, and Content providers uses data similar to tables in the database

Exposed. Content providers stores and retrieves data, allowing all applications to access

This is the only way to share data between applications.

To make the data of an application public, you can create one of your own

Content provider or add your data to an existing Content provider

The premise is that you have the same data type and the permission to write Content provider.

How can I obtain data exposed by other applications through a set of standard and unified interfaces?

Android provides ContentResolver, which allows external programs to use

The ContentResolver interface accesses the data provided by ContentProvider.

 

16. If there is a return statement in try {}, will the code in finally {} following this try be executed? When will it be executed? Before or after return

Will be executed, execute before return

 

17. What are the advantages of IntentService?

When processing Intent, a corresponding Service is generated;

The Android process processor will not kill you as much as possible; it is very easy to use

 

18. If the background Activity is recycled by the system for some reason, How can I save the current status before being recycled by the system?

Override the onSaveInstanceState () method to save the data to be saved,

This method will be called before the activity is recycled. By Rewriting onRestoreInstanceState ()

Method to extract the saved data

 

19. How to set an Activity as a window style.

: <Activity> Configuration: android: theme = "@ android: style/Theme. Dialog"

 

20. Talk about the Android IPC (inter-process communication) Mechanism

IPC is short for internal process communication and is a resource that shares "named pipes. IPC in Android

The Mechanism is to enable interaction between the Activity and Service at any time.

This mechanism is only applicable to the communication between Activity and Service, similar to remote method calls,

Similar to access in C/S mode. Define the IPC interface by defining the AIDL interface file. Servier

Implement the IPC interface. The Client calls the local proxy of the IPC interface.

 

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.