Android Service instance telephone listener (1)

Source: Internet
Author: User

Step 1: Inherit the Service class

 
 
  1. public class SMSService extends Service { } 

Step 2: configure the service on the <application> node in the AndroidManifest. xml file:

 
 
  1. <service android:name=".SMSService" /> 

The service cannot run on its own and needs to be calledContext. startService () or Context. bindService ()Method to start the service. Both methods can start the Service, but their usage is different. The startService () method is used to enable the service. There is no relation between the caller and the service. Even if the caller exits, the Service continues to run. When the bindService () method is used to enable the Service, the caller and the service are bound together. Once the caller exits, the service is terminated. This feature features that the caller does not want to generate at the same time and must die at the same time.

If you plan to use the Context. startService () method to start the service, when the service is not created, the system will first call the onCreate () method of the service and then call the onStart () method. If the service has been created before the startService () method is called, multiple call of the startService () method will not lead to multiple creation of the service, but will lead to multiple calls of the onStart () method. A service started using the startService () method can only end the service by calling the Context. stopService () method. The onDestroy () method is called when the service ends.

If you plan to use the Context. bindService () method to start the service, when the service is not created, the system will first call the onCreate () method of the service and then call the onBind () method. At this time, the caller and the service are bound together. After the caller exits, the system will first call the onUnbind () method of the service and then call 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 the caller wants to unbind from the service being bound, he can call the unbindService () method. Calling this method also causes the system to call the onUnbind () --> onDestroy () method of the service.

Common lifecycle callback methods for services are as follows:

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.

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. Although the startService () method is called multiple times, 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 binds to the service. When the caller and the service are already bound, multiple calls to the Context. bindService () method will not cause this method to be called multiple times.

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.

You can use your mobile phone to perform on-site recording as follows:

Step 1: add the audio burning permission to the function list file AndroidManifest. xml:

 
 
  1. <uses-permission android:name="android.permission.RECORD_AUDIO"/> 

Step 2: Compile the audio burning code:

MediaRecorder recorder = new MediaRecorder ();

Recorder. setAudioSource (MediaRecorder. AudioSource. MIC); // collect sound from the microphone

Recorder. setOutputFormat (MediaRecorder. OutputFormat. THREE_GPP); // content output format

Recorder. setAudioEncoder (MediaRecorder. AudioEncoder. AMR_NB); // Audio Encoding Method

Recorder. setOutputFile ("/sdcard/itcast. amr ");

Recorder. prepare (); // expected preparation

Recorder. start (); // starts to burn
...
Recorder. stop (); // stop burning

Recorder. reset (); // reset

Recorder. release (); // resources must be released after burning.


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.