Android interview questions (2): Android interview questions

Source: Internet
Author: User

Android interview questions (2): Android interview questions

This is a continuation of the previous part of the knowledge, still interview questions, enter the subject:

 

Interview Question 1: your understanding of the Activity:

This is what the examiner wants you to write your own experiences and experiences when you are working on a project. We can answer the question about how to handle an Activity exception when it is recycled by the system. Then we can talk about the lifecycle of the Activity.

 

Question 2: Is the Service running in the main thread? Can I perform time-consuming operations in the Service?

A: The service runs on the main thread by default. Many people think that the backend service should be a subthread, but it is not. Therefore, we cannot perform time-consuming operations in it, an ANR exception occurs. We need to create a new thread for time-consuming operations.

Of course, we can also specify the service to be executed in the specified thread. Generally, we specify the thread When configuring the configuration file:

Java code
  1. <Service android: process = "cn. itcast. xxx"> </service>

 

 

Interview question 3: How to transmit data between two activities?

A: If it is a basic data type, we can transmit data through Intent or a bundle through intent putExtra. To obtain data from other intent, we can use:

Java code
  1. Intent intent = getIntent ();
  2. Bundle bundle = intent. getExtras ();

 

Bundle is similar to a map set.

Java code
  1. Intent. getStringExtra ("key", "value ");
  2. Intent. getBooleanExtra ("key", "value ")

 

However, if it is a complex type, we can store objects in the Application global and implement the class of our own application. The application of the basic system can be obtained by every activity, of course, remember to configure it in the configuration file.

To implement the transfer of complex types, you can also enable an object to implement the Serializable interface, so that each interface can be serialized to a file. The specific implementation process is to instantiate a FileOutputStream and an ObjectOutputStream, then write data through the object output to the file. In another Activity, we can read the data of this object through the object, so that we can:

Java code
  1. File file = new File ("c: \ 1.obj ");
  2. FileOutputStream fos = new FileOutputStream (file );
  3. ObjectOutputStream oos = new ObjectOutputStream (fos );
  4. Student stu = new Student ();
  5. Stu. setId ("10001 ");
  6. Stu. setName ("zs ");
  7. Oos. writeObject (stu );
  8. FileInputStream FCM = new FileInputStream (file );
  9. ObjectInputStream ois = new ObjectInputStream (FCM );
  10. Student stu1 = (Student) ois. readObject ();
  11. System. out. println (stu1.getName ());

 

 

The above uses Serializable to serialize an object to a file and the reading efficiency is not high. If it is serialized to the memory, the efficiency is high. Google then provides the parcelable interface, parcelable serializes objects to a common memory space in the android operating system.

In fact, we can also use Gson to pass the object. Similar to json, We can package the object into a string in a certain format, and then use intent to pass the same basic data type before deserialization to get the data:

Java code
  1. Gson gson = new Gson ();
  2. List <Person> persons = new ArrayList <Person> ();
  3. For (int I = 0; I <10; I ++ ){
  4. Person p = new Person ();
  5. P. setName ("name" + I );
  6. P. setAge (I * 5 );
  7. Persons. add (p );
  8. }
  9. String str = gson. toJson (persons );

 

Then we get the str string and take a single object:

Java code
  1. Person person = gson. fromJson (str, Person. class );

 

Object collection:

Java code
  1. List <Person> ps = gson. fromJson (str, new TypeToken <List <Person> () {}. getType ());
  2. For (int I = 0; I <ps. size (); I ++)
  3. {
  4. Person p = ps. get (I );
  5. System. out. println (p. toString ());
  6. }

 

But if we want to pass a large file, we can't use the above method, because the efficiency is too low, we will pass a reference in the past, through uri:

Java code
  1. // Transfer Data Reference
  2. Intent. setData (Uri)
  3. Uri. fromFile (); // transfer of large images
  4. ContentResolver. getInputStream (uri); ----- store data
  5. ContentResolver. getOutputStream (uri); ---- get data

 

 

Interview question 4: how to start a service when starting an Activity?

In the OnCreate method, new Intent (the service to be started); then startService (intent );.

 

Interview question 5: Can different activities of the same program be stored in different Task stacks?

This is fine. We can set flag for intent when activating a new activity:

Add FLAG_ACTIVITY_NEW_TASK singleinstance to the FLAG_ACTIVITY_NEW_TASK singleinstance flag.

And the newly opened Activity stack will be in the separate task stack.

Java code
  1. Intent intent = new Intent (A. this, B. class );
  2. Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK );
  3. StartActivity (intent );

 

When we enable the new task stack, we have set it to enable the new task stack. In the onCreate method of the Activity, even if a new task stack is enabled, it will not be enabled again.

 

Question 5: How does the Activity bind to the Service? How does one start its own service in the activity?

We write services in different Service methods, and the effects are different.

In StartService (), once created and called, the methods in the service cannot be used.

BindService () binds the service to the caller. If the caller is destroyed, the service is destroyed. At the same time, we can also use the method in to start the service by rewriting the bandService: we need to write our own ServiceConnection object MyConn (override onServiceConnected and OnServiceDisconnected) and BIND_AUTO_CREATE .:

Java code
  1. Private class Myconn implements ServiceConnection
  2. {
  3. Public void onServiceConnected (ComponentName name, IBinder service ){
  4. // TODO Auto-generated method stub
  5. // You can use the methods in the service through the IBinder object.
  6. }
  7. Public void onServiceDisconnected (ComponentName name ){
  8. // TODO Auto-generated method stub
  9. }
  10. }

 

 

Interview question 6: What is the service life cycle? How can I start a service? What are the differences between these methods? How can I disable the service?

In the Service life cycle, because there is no interface, the callback method is less than the Activity, only onCreate, onStart, onDestroy, method, because it is a Service, so there are onBind and onUnbind methods.

There are usually two ways to start a Service. Their impact on the Service lifecycle is different.

1. Use startService

As mentioned above, StartService is not bound with the caller. The Service goes through onCreate to onStart and is running. The onDestroy method is called during stopService.

If the caller directly exits without calling stopService, the Service will continue to run in the background.

2. bindService

This method is bound with the caller. The Service runs onCreate and then calls onBind. When the caller exits, Srevice calls onUnbind-> onDestroyed. The so-called binding together will survive. The caller can also call the unbindService method to stop the service. At this time, Srevice will call the onUnbind-> onDestroyed method.

But what happens if several methods are intertwined?

One principle is that the onCreate method of the Service will be called only once, that is, no matter how many times startService and bindService, the Service will be created only once.

If bind is started, the onStart method of Service is run directly. If start is started, the onBind method is run directly during bind.

If the bindService is called during service running and the stopService is called again, the service will not call the onDestroy method, and the service will stop. You can only call the UnbindService and the service will be destroyed.

If a service is called multiple times after it is started through startService, the service will call the onStart method multiple times. If you call stopService multiple times, the service only calls the onDestroyed method once.

If a service is called multiple times after it is started through bindService, the service calls the onBind method only once.

If unbindService is called multiple times, an exception is thrown.

 

Interview question 7: No service is required. The page B is used for playing the music. The page B is redirected from A to B and then returns. How can I continue playing the music?

This is not a good question. In fact, it is not processed by default, and the music in B can be played.

When you encounter a problem, you can adapt to the problem at random and give full play to it flexibly. For example, you can say this to your understanding of startActivityForResult ()

When A enables B, it uses the startActivityForResult () method. When B returns, it returns the playback status information to A and A to continue playing the music.

 

Interview question 8: What is IntentService? What are the advantages?

In simple terms, compared with the general service, he has a thread. We process time-consuming operations in OnHandleIntent. It is a convenient service class provided by the Sdk with asynchronous processing and the OnHandleIntent () method of asynchronous processing (). Each time the OnHandleIntent method is executed, a different thread is enabled.

 

Interview Question 9: when to use Service?

The front-end process has the highest priority and is the most difficult to be killed. The service is only the first priority of the front-end process. Using the service can make it difficult for the processes running in the background to be killed.

1. if the service is calling the onCreate, onStartCommand, or onDestory method, the process used for the current service is equivalent to the foreground process to avoid being killed.

2. if the current service has been started, processes with it have lower priority than processes visible to users, but are more important than invisible processes, this means that the service is generally not killed.

3. If a foreground process is bound to a service at the same time, the process has the highest priority. It can be understood that the service is visible.

4. if the service can use the startForeground (true) method to set the service to the foreground state, which is equivalent to forcibly increasing the priority of the service, the system considers it visible to the user, it will not killed when the memory is insufficient.

Where is the service used?

1. The features of the Service allow him to run in the background all the time. He can create a thread in the service to complete time-consuming operations. The weather forecast widget TimerTask Timer regularly executes timertask.

2. after a Broadcast receiver captures an event, it can start a service to complete a time-consuming operation. the Broadcast receiver has a short life cycle and response time. It takes only 10 seconds to complete.

 

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.