Android interview (III)

Source: Internet
Author: User

1. Please talk about the Android system architecture.
A: The Android system adopts a layered architecture. The application layer, application framework layer, system Runtime Library layer, and Linux core layer are from the top to the bottom.

2. Let's talk about five common la s for Android.
A: There are five layout modes in Android: framelayout (Framework layout), linearlayout (linear layout), absolutelayout (absolute layout), and relativelayout (relative layout ), tablelayout (table layout ).
(1) framelayout framework layout. All elements in the framework are placed in the leftmost area, and they cannot be specified with an exact position, the next child element overwrites the previous child element, which is suitable for browsing a single image.
(2) linearlayout linear layout is the most common layout method in applications. It mainly provides models for horizontal or vertical control arrangement. Each sub-component is located vertically or horizontally. (vertical by default)
(3) absolutelayout is an absolute positioning layout. The component is located in the coordinate axis. The upper left corner is (0, 0), and the X axis increments to the right. The component positioning attribute is Android: layout_x and Android: layout_y to determine the coordinates.
(4) Relative layout of relativelayout: determine the location of the next component based on another component or top-layer parent component. It is similar to that in CSS.
(5) tablelayout table layout, similar to the table in HTML. tablerow is used for layout, where tablerow represents a row, and each view component of tablerow represents a cell.

3. Talk about the android data storage method.
A: Android provides five data storage methods:
(1) Use sharedpreferences to store data. It is a mechanism provided by Android to store some simple configuration information and uses XML format to store data to devices. It can only be used in the same package and cannot be used between different packages.
(2) file storage data. File storage is a common method for reading/writing files in Android, similar to the program that implements I/O in Java, openfileinput () and openfileoutput () methods are provided to read files on the device.
(3) SQLite database stores data. SQLite is a standard database in Android. It supports SQL statements and is a lightweight embedded database.
(4) use contentprovider to store data. It is mainly used for data exchange between applications, so that other applications can save or read various data types of this content provider.
(5) network storage data; upload (store) and download (obtain) the data in the network space provided to us through the network.

4. What are the differences between activity, intent, content provider, and service in Android.
A: activity is the most basic Android Application component. An activity is a single screen. Each activity is implemented as an independent class and inherited from the activity base class.
Intent: intent, which describes what the application wants. The most important part is the data corresponding to actions.
Content Provider: content provider. Android applications can save their data to files, SQLite databases, or even any valid device. When you want to share your application data with other applications, the content provider can play a role.
Service: a service with a long life cycle and no user interface.

5. What are the differences between view, surfaceview, and glsurfaceview.
A: view is the most basic. You must update the screen in the main UI thread, which is slow.
Surfaceview is a subclass of view. Similar to the double easing mechanism, surfaceview updates the screen in the new thread, so refreshing the interface is faster than the view.
Glsurfaceview is a subclass of surfaceview and is dedicated to OpenGL.

6. What is the function of adapter? What are common adapters?
A: The adapter is an adapter interface connecting back-end data and front-end display. Common adapters include arrayadapter, baseadapter, cursoradapter, headerviewlistadapter, listadapter, resourcecursoradapter, simpleadapter, simplecursoradapter, spinneradapter, and wrapperlistadapter.

7. What information does the manifest. xml file mainly contain?
A: manifest: root node, which describes all contents in the package.
Uses-permission: Requests Security licenses required for normal operation of your package.
Permission: declares a security license to restrict which programs can be used for components and functions in your package.
Instrumentation: Declares the code used to test this package or other package command components.
Application: contains the root node declared by the application-level component in the package.
Activity: activity is the main tool used to interact with users.
Receiver: changes or operations that an intentreceiver can perform on an application to obtain data, even if it is not currently running.
Service: A service is a component that can run at any time in the background.
Provider: contentprovider is a component used to manage persistent data and publish it to other applications.

8. Write a piece of code (SAX, Dom, or pull) to parse the XML document.
A: The XML file to be parsed is as follows:

Zhang San
22. Li Si
23 define a person an named person to store the XML content parsed above
Public class person {
Private integer ID;
Private string name;
Private short age;

Public integer GETID (){
Return ID;
}

Public void setid (integer ID ){
This. ID = ID;
}

Public String getname (){
Return name;
}

Public void setname (string name ){
This. Name = Name;
}

Public short getage (){
Return age;
}

Public void setage (short age ){
This. Age = age;
}
}
(1) use SAX to read XML files. It uses event-driven and does not need to parse the entire document. It is fast and occupies less memory. You need to provide the contenthandler interface class for Sax.
Persondefaulthandler. Java
Import java. util. arraylist;
Import java. util. List;

Import org. xml. Sax. attributes;
Import org. xml. Sax. saxexception;
Import org. xml. Sax. helpers. defaulthandler;

Import com. sinber. domain. person;

Public class persondefaulthandler extends defaulthandler {
Private list persons;
Private person; // record the current person
Private string pertag; // record the name of the previous tag

/**
* Override the start document method of the parent class. Used for initialization
*/
@ Override
Public void startdocument () throws saxexception {
Persons = new arraylist ();
}

@ Override
Public void startelement (string Uri, string localname, string QNAME,
Attributes attributes) throws saxexception {
If ("person". Equals (localname )){
Integer id = new INTEGER (attributes. getvalue (0); // obtain the ID
Person = new person ();
Person. setid (ID );
}
Pertag = localname;
}

/** Parameters:
* Ch the entire XML string
* Index position of the Start Node value in the entire XML string
* Length the length of the node Value
*/
@ Override
Public void characters (char [] CH, int start, int length)
Throws saxexception {
If (pertag! = NULL ){
String data = new string (CH, start, length );
If ("name". Equals (pertag )){
Person. setname (data );
} Else if ("Age". Equals (pertag )){
Person. setage (new short (data ));
}
}
}

@ Override
Public void endelement (string Uri, string localname, string QNAME)
Throws saxexception {
If ("person". Equals (localname )){
Persons. Add (person );
Person = NULL;
}
Pertag = NULL;
}

Public list getpersons (){
Return persons;
}
}
Saxperson. Java
Import java. Io. inputstream;
Import java. util. List;
Import javax. xml. parsers. saxparser;
Import javax. xml. parsers. saxparserfactory;
Import com. sinber. domain. person;
Public class saxperson {
Public static list getperson () throws exception {
// Get the file through the Class Loader
Inputstream instream = saxpersonservice. Class. getclassloader (). getresourceasstream ("person. xml ");
Saxparserfactory factory = saxparserfactory. newinstance ();
Saxparser = factory. newsaxparser ();
Persondefaulthandler handler = new persondefaulthandler ();
Saxparser. parse (instream, Handler );
Instream. Close ();

Return handler. getpersons ();
}
}

(2) When Dom parses an XML file, it reads all the content of the XML file to the memory, and then allows you to use the dom api to traverse the XML tree and retrieve the required data.
Domperson. Java

Import java. Io. inputstream;
Import java. util. arraylist;
Import java. util. List;

Import javax. xml. parsers. documentbuilder;
Import javax. xml. parsers. documentbuilderfactory;
Import org. W3C. Dom. Document;
Import org. W3C. Dom. element;
Import org. W3C. Dom. node;
Import org. W3C. Dom. nodelist;
Import com. sinber. domain. person;
Public class domperson {
Public static list getperson () throws exception {
List pers = new arraylist ();
Inputstream instream = saxpersonservice. Class. getclassloader (). getresourceasstream ("person. xml ");
Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
Documentbuilder builder = factory. newdocumentbuilder ();
Document dom = builder. parse (instream );
Element root = Dom. getdocumentelement ();
Nodelist persons = root. getelementsbytagname ("person ");
For (INT I = 0; I <persons. getlength (); I ++ ){
Element personnode = (element) Persons. item (I );
Person = new person ();
Person. setid (New INTEGER (personnode. getattribute ("ID ")));
Nodelist childnodes = personnode. getchildnodes ();
For (Int J = 0; j <childnodes. getlength (); j ++ ){
Node childnode = childnodes. Item (j );
If (childnode. getnodetype () = node. element_node ){
Element element = (element) childnode;
If ("name". Equals (childnode. getnodename ())){
Person. setname (new string (element. getfirstchild (). getnodevalue ()));
} Else if ("Age". Equals (childnode. getnodename ())){
Person. setage (new short (element. getfirstchild (). getnodevalue ()));
}
}
}
Pers. Add (person );
}
Instream. Close ();
Return pers;
}
}
(3) Use the pull parser to read XML files
Pullperson. Java
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. util. arraylist;
Import java. util. List;
Import org. xmlpull. v1.xmlpullparser;
Import org. xmlpull. v1.xmlserializer;
Import Android. OS. environment;
Import Android. util. xml;
Import com. sinber. domain. person;
Public class pullperson {

Public static void save (list persons) throws exception {
Xmlserializer serializer = xml. newserializer ();
File file = new file (environment. getexternalstoragedirectory (), "person. xml ");
Fileoutputstream outstream = new fileoutputstream (File );
Serializer. setoutput (outstream, "UTF-8 ″);
Serializer. startdocument (UTF-8 ", true );
Serializer. starttag ("", "persons ");
For (person: Persons ){
Serializer. starttag ("", "person"); // person
Serializer. Attribute ("", "ID", "" + person. GETID ());
Serializer. starttag ("", "name"); // name
Serializer. Text (person. getname ());
Serializer. endtag ("", "name"); // name
Serializer. starttag ("", "age"); // age
Serializer. Text (person. getage (). tostring ());
Serializer. endtag ("", "age"); // age

Serializer. endtag ("", "person"); // person
}
Serializer. endtag ("", "persons ");
Serializer. enddocument ();
Outstream. Close ();
}

Public static list getpersons () throws exception {
List persons = NULL;
Person = NULL;
Xmlpullparser parser = xml. newpullparser ();
Inputstream instream = pullpersonservice. Class. getclassloader (). getresourceasstream ("person. xml ");
Parser. setinput (instream, "UTF-8 ″);
Int eventtype = parser. geteventtype (); // triggers the first event
While (eventtype! = Xmlpullparser. end_document ){
Switch (eventtype ){
Case xmlpullparser. start_document:
Persons = new arraylist ();
Break;

Case xmlpullparser. start_tag: // start element event
If ("person". Equals (parser. getname ())){
Person = new person ();
Person. setid (New INTEGER (parser. getattributevalue (0 )));
} Else if (person! = NULL ){
If ("name". Equals (parser. getname ())){
Person. setname (parser. nexttext ());
} Else if ("Age". Equals (parser. getname ())){
Person. setage (new short (parser. nexttext ()));
}
}
Break;

Case xmlpullparser. end_tag: // End Element event
If ("person". Equals (parser. getname ())){
Persons. Add (person );
Person = NULL;
}
Break;

Default:
Break;
}
Eventtype = parser. Next ();
}
Return persons;
}
}
You can choose one of the above three methods.

9. Describe Android Digital Signature Based on your understanding.
A: (1) All applications must have digital certificates. Android does not install an application without digital certificates.
(2) the digital certificate used by the android package can be self-Signed and does not require signature authentication by an authoritative Digital Certificate Authority.
(3) To officially release an Android app, you must use a digital certificate generated by a suitable private key to sign the app, rather than using the debugging certificate generated by the ADT plug-in or ant tool for release.
(4) digital certificates are valid. Android only checks the validity period of the certificate when the application is installed. If the program has been installed in the system, the normal functions of the program will not be affected even if the certificate expires.
10. If the head of the single-chain table is known, write a function to reverse the chain table.
A:
Node. Java
Public class node {
Private integer count;
Private node nextnode;

Public node (){

}
Public node (INT count ){
This. Count = new INTEGER (count );
}
Public integer getcount (){
Return count;
}
Public void setcount (integer count ){
This. Count = count;
}
Public node getnextnode (){
Return nextnode;
}
Public void setnextnode (node nextnode ){
This. nextnode = nextnode;
}

}
Reversesinglelink. Java
Public class reversesinglelink {
Public static node revsinglelink (node head ){
If (Head = NULL) {// The linked list is empty and cannot be in reverse order.
Return head;
}
If (head. getnextnode () = NULL) {// if there is only one node, the inverse is also the same
Return head;
}
Node rhead = revsinglelink (head. getnextnode ());
Head. getnextnode (). setnextnode (head );
Head. setnextnode (null );
Return rhead;
}
Public static void main (string [] ARGs ){
Node head = new node (0 );
Node temp1 = NULL, temp2 = NULL;
For (INT I = 1; I <100; I ++ ){
Temp1 = new node (I );
If (I = 1 ){
Head. setnextnode (temp1 );
} Else {
Temp2.setnextnode (temp1 );
}
Temp2 = temp1;
}
Head = revsinglelink (head );
While (Head! = NULL ){
Head = head. getnextnode ();
}
}
}

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.