This is the continuation of the last part of the knowledge, is still the interview questions, into the topic:
Interview question one: Talk about your understanding of activity:
This is what the examiner wants you to write about your experience, when you do the project, and we can answer how the activity is handled when the system is recycled, and then we can talk about the life cycle of the activity.
Interview question two: is the service running on the main thread? Can I perform time-consuming operations inside the service?
A: The service is run by default in the main thread, many people think that the backend service should be in the sub-thread, actually not, so we can not perform time-consuming operation inside, because it will produce a ANR exception. We need to do time-consuming operations to open a new thread and handle time-consuming operations.
Of course, we can also specify that the service executes in the specified thread, typically we specify the thread at the time the manifest file is configured:
Java code
- <service android:process= "Cn.itcast.xxx" ></service>
Interview question three: How to pass data between two activity?
A: If it is a basic data type, we can pass the data through intent, or pass a bundle through intent Putextra, the other intent want to get the data, can pass:
Java code
- Intent Intent = Getintent ();
- Bundle bundle = Intent.getextras ();
Bundle like a collection of maps
Java code
- Intent.getstringextra ("Key", "value");
- Intent.getbooleanextra ("Key", "value")
However, if it is a complex type, we can store objects in the application global, to implement their own application of this class, the basic system of application, each activity can be taken, of course, remember the list file configuration.
The implementation of a complex type of delivery can also be achieved by allowing an object to implement the Serializable interface, which can be serialized to a file after each interface, the implementation process is to instantiate a fileoutputstream and a objectoutputstream, The object output is then streamed to the file to write the data. In another activity, we read this object data through objects, so we can do this:
Java code
- File File = new file ("C:\\1.obj");
- FileOutputStream fos = new FileOutputStream (file);
- ObjectOutputStream oos = new ObjectOutputStream (FOS);
- Student stu = new Student ();
- Stu.setid ("10001");
- Stu.setname ("Zs");
- Oos.writeobject (Stu);
- FileInputStream fis = new FileInputStream (file);
- ObjectInputStream ois = new ObjectInputStream (FIS);
- Student stu1 = (Student) ois.readobject ();
- System.out.println (Stu1.getname ());
The above through serializable the object serialization to the file and then read the efficiency is not high, the introduction if the serialization to memory efficiency is high, Google then provides the parcelable interface, parcelable object serialization to the Android operating system A common piece of memory space.
In fact, we can also use Gson, similar to JSON to wrap objects into strings in a certain format, and then deserialize the data by intent passing the basic data type:
Java code
- Gson Gson = new Gson ();
- list<person> persons = new arraylist<person> ();
- for (int i = 0; i < i++) {
- Person p = new person ();
- P.setname ("name" + i);
- P.setage (i * 5);
- Persons.add (P);
- }
- String str = gson.tojson (persons);
Then we get this str string, taking a single object:
Java code
- Person person = Gson.fromjson (str, person.class);
To take a collection of objects:
Java code
- list<person> PS = Gson.fromjson (str, new typetoken<list<person>> () {}.gettype ());
- for (int i = 0; i < ps.size (); i++)
- {
- Person p = ps.get (i);
- System.out.println (P.tostring ());
- }
But if we are going to pass a large file, we cannot use the above method, because the efficiency is too low, we will pass a reference to the past, through the URI:
Java code
- Passing a reference to the data
- Intent.setdata (Uri)
- Uri.fromfile (); Transfer of large pictures
- Contentresolver.getinputstream (URI);-----Save Data
- Contentresolver.getoutputstream (URI);----Fetch data
Interview question four: How to start a service when an activity is started?
We are inside the OnCreate method new Intent (service to start); StartService (Intent);.
Interview question five: the same program, but can different activity be placed in different task stack?
This is possible, and we can set flag for intent when activating a new activity:
Intent flag Add flag_activity_new_task singleinstance separate task stack
, and the new activity stack will be in a separate task stack.
Java code
- Intent Intent = new Intent (a.this,b.class);
- Intent.setflags (Intent.flag_activity_new_task);
- StartActivity (Intent);
When we set up a new task stack when we open it, we will not open it again, even if we write a new task stack in the activity's OnCreate method.
Interview question five: how the activity and service binding, how to start their own service in the activity?
We write services in different ways of service, the effect is not the same.
StartService () inside the word, once it is created call the method that does not have to use the service inside.
Bindservice () binds the service to the caller, and if the caller is destroyed, the service is destroyed. At the same time, we can use the method inside to start the service through the Bandservice: we need to write a Serviceconnection object myconn, ( Override the Onserviceconnected and Onservicedisconnected methods) and bind_auto_create.:
Java code
- Private class MyConn implements Serviceconnection
- {
- public void onserviceconnected (componentname name, IBinder service) {
- TODO auto-generated Method Stub
- You can use the methods inside the service through IBinder objects.
- }
- public void onservicedisconnected (componentname name) {
- TODO auto-generated Method Stub
- }
- }
Interview Six: What is the life cycle of service,service? How to start a service, what is the difference between these methods, how to deactivate service?
In the service life cycle, because there is no interface, the method is less than 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, and their impact on the service life cycle is different.
1. by StartService
As mentioned earlier, StartService, without being bound to the caller, the service will go through onCreate to OnStart and then be in the running state, StopService call the OnDestroy method.
If the caller exits directly without calling StopService, the service will run in the background.
2. By Bindservice
This method is bound to the caller, the service runs OnCreate, and then calls Onbind, and the caller exits, Srevice calls the Onunbind->ondestroyed method. The so-called binding together is a common survival. The caller can also stop the service by calling the Unbindservice method, when Srevice calls the Onunbind->ondestroyed method.
But what happens when there are several ways to weave together?
One principle is that the OnCreate method of service will only be called once, that is, how many times you startservice and Bindservice,service are created only once.
If BIND is first, then start will run the service's OnStart method directly, if first start, then bind will run the Onbind method directly.
If the service runs during the call of Bindservice, then call StopService, the service will not call the OnDestroy method, the service stop is not dropped, can only call Unbindservice, The service will be destroyed.
If a service calls StartService multiple times after StartService is start, the service calls the OnStart method multiple times. If you call StopService multiple times, the service will only call the Ondestroyed method once.
If a service calls Bindservice multiple times after Bindservice is start, the service will only invoke the Onbind method once.
If you call Unbindservice multiple times, an exception will be thrown.
Interview Seven: no service,b page for music playback, from a jump to B, and then return, how to make music continue to play?
This question is not very good, in fact, the default does not do any processing, b in the music can be played.
Encounter problems, you can improvise, flexible play, think more about the details, such as this question can say, say your understanding of Startactivityforresult ()
A When you turn on B, use the Startactivityforresult () method, B returns the status information to a, a to continue playing music.
Interview question eight: what is Intentservice? What are the advantages?
Simply speaking, he has a thread on his own, relative to the General Service, and we handle time-consuming operations in the onhandleintent (). It is the convenience of the SDK to provide us with asynchronous processing of the service class, asynchronous processing Method Onhandleintent (). Each time the Onhandleintent method is executed, a different thread is turned on.
Interview nine: When to use service?
The foreground process has the highest priority, the hardest to be killed, the service is only priority 1th than the foreground process, and the service allows us to run the process in the background is not easy to hang out.
1. If the service is calling OnCreate, Onstartcommand, or Ondestory methods, then 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 (start), the process that owns it has a lower priority than those visible to the user, but is more important than the invisible process, which 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 equates to a forced increase in the priority of the service, then the system is considered to be visible to the user and does not killed when the memory is low.
Where is the service used?
The 1.Service feature allows him to run in the background, creating threads within the service to perform time-consuming operations. Weather widget TimerTask Timer executes timertask regularly.
2.Broadcast receiver After capturing an event, a service can be used to complete a time-consuming operation. Broadcast receiver life cycle and response time is short, only 10 seconds, not executed after the execution.
- If the remote service is started, it can be bind multiple times, but will not re-create. Sony Ericsson mobile phone x10i face recognition service can be used by the gallery, can be used by cameras, cameras and other programs.