Service and the use of services for telephone eavesdropping

Source: Internet
Author: User

Service Introduction
Some operations are time-consuming (such as file I/O operations, database operations with a large amount of data, and network access). We hope they will run in the background without delaying the current operations, this introduces the concept of Service.
Services in Android are similar to services in windows. Generally, there is no user operation interface, and it is not easy to detect when running in the system.

Service lifecycle callback Method

When the Context. startService () method is used to start the service, the related lifecycle Method
OnCreate () -----> onStartCommand () -----> onDestroy ()
OnCreate () This method is called when a service is created. This method is called only once, no matter how many times the startService () or bindService () method is called, the service is created only once !!!
OnStartCommand () 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.
Although the startService () method is called multiple times, the onStartCommand () method is called multiple times.
OnDestroy () This method is called when the service is terminated.

 

When the Context. bindService () method is used to start the service, the related lifecycle Method
OnCreate () -----> onBind () -----> onUnbind () -----> onDestroy ()
OnBind () calls back this method only when the Context. bindService () method is used to start the service.
This method is called when the caller binds to the service. When the caller binds to the service, the Context is called multiple times. bindService () does not cause this method to be called multiple times, and the service is created only once !!!
Onunbind () calls back this method only when the context. bindservice () method is used to start the service. This method is called when the caller and the service are unbound.

Train of Thought for implementing Telephone listening in service:
1. Create a broadcast receiver to receive the boot start broadcast from the system.
2. After receiving the boot broadcast, we use an intent in the receiver to activate the service.
3. Start recording upon incoming call. After hanging up, upload the recording to the network.

Steps:
1. Custom phoneservice inherits from service
2. Complete component registration and permission declaration in the inventory file
3. Complete the code

Note:
1. You can call the context. startservice () or context. bindservice () method to start the service. Both methods can start the service, but the usage is different.
The startservice () method is used to enable the service. There is no relation between the visitor and the service. Even if the visitor exits, the service still runs.
Use the bindservice () method to enable the Service. When a visitor is bound to the service, the service is terminated once the visitor exits.
2. Start and close the service through startservice () and stopservice (). This is applicable to the absence of interaction between the service and its visitors.
If a method call or parameter is required between the service and the visitor, bindservice () and unbindservice () must be used to start and close the service.

Use context. the bindservice () method starts the service. when the service is not created, the system first calls the oncreate () method of the service and then calls the onbind () method. At this time, the visitor and the service are bound together.

If the visitor wants to communicate with the service, the onBind () method must return the Ibinder object. if the visitor exits, the system first calls the onUnbind () method of the service and then calls the onDestroy () method.
If the service has been bound before the bindService () method is called, multiple calls to the bindService () method will not result in multiple service creation and binding (that is, onCreate () and onBind () method is not called multiple times ).
If a visitor wants to unbind from the service being bound, the unbindService () method can be called. Calling this method also causes the system to call the onUnbind () and onDestroy () Methods of the service.

/// // The following is the addendum of the Service note, course from Sundy //////////////////////////////////// /////////

Service is not an independent thread (process)

After the service is started, press Home to continue running the service.
After the service is started, the process is exited and the service does not continue to run.
When a service is started without multithreading, the interface will be congested. For example, if you click a button in an Activity to start a service, the service will continuously use Log for information output within 100 seconds.
At this time, we press the return key or press the return key multiple times, which will cause ANR errors.
However, if we enable a new thread in the service to output information using logs, it will not cause ANR errors.

StartService () is usually started only and does not require a return value. Therefore, it can be used to perform network upload or download operations. After the operation is complete, it is automatically stopped.

StartService () starts the service. If you enable a thread in the public void onCreate () method of the Service to perform some operations
At this time, there is such a problem: although the service is terminated and the onDestroy () method of the service is executed, the thread is still working ........................

In addition to this problem, sometimes the service is inexplicably blocked.
Therefore, the android system introduces IntentService, which is mainly used to execute asynchronous requests, and Handle is also used in it.

Another problem:
The Service runs in the background and has a low priority. When the memory is insufficient, the system will kill the Service.
Solution: run the service on the front-end
Method: startForeground (int id, Notification)

Knowledge point:
Native service (internal: C ++)
This method can be called when startService () and bindService are both difficult to use and need to run in the background for a long time.
To call this method, you must use the NDK plug-in to develop C ++.
But how does Java call a class library written in C ++ ??? A: Through JNI (java native interface is the native interface of java)

Service and thread Selection
When the current operation is to start a more independent operation, you can call the thread. You just need to download something.

Stub is a pointer pointing to a memory block, that is, the pointer of Stub as a pointer.

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.