Android four major component application series - realize phone call interception and phone call recording

Android four major component application series - realize phone call interception and phone call recording

[[152918]]

Use BordercastReceiver and Service components to implement the following functions:

1. When the phone is in the state of an incoming call, start the monitoring service to monitor and record the incoming call.

2. Set up a phone blacklist. When the incoming call is from the blacklist, hang up directly.

When a call is made or the phone status changes, the system will issue an ordered broadcast, so we can use BordercastReceiver to receive the broadcast. Since BordercastReceiver has a short execution time and cannot perform time-consuming tasks or use child threads, we should start a Service to listen for calls and process them.
2. Add AIDL file

Android does not have an open API for ending a call. To end a call, you must use AIDL to communicate with the phone management service and call the API in the service to end the call. To do this, you need to add the Android source code files NeighboringCellInfo.aidl and ITelephony.aidl to the project, as shown in the figure:

Android Studio will automatically compile and generate the corresponding class files
3. Write TelReceiver component

  1. public   class TelReceiver extends BroadcastReceiver {
  2. public TelReceiver() {
  3. }
  4.  
  5. @Override  
  6. public   void onReceive(Context context, Intent intent) {
  7. Intent i= new Intent(context,ListenPhoneService. class );
  8. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  9. i.setAction(intent.getAction());
  10. i.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER,
  11. intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)); //Phone number  
  12. i.putExtra(TelephonyManager.EXTRA_STATE,
  13. intent.getStringExtra(TelephonyManager.EXTRA_STATE)); //Phone status  
  14. context.startService(i); //Start the service  
  15. }
  16. }

Register broadcast:

  1. <receiver android:name= ".TelReceiver" >
  2. <intent-filter android:priority= "1000" >
  3. <action android:name= "android.intent.action.PHONE_STATE" />
  4. <action android:name= "android.intent.action.NEW_OUTGOING_CALL" />
  5. </intent-filter>
  6. </receiver>
4. Write the ListenPhoneService component

  1. public   class ListenPhoneService extends Service {
  2. private AudioManager mAudioManager;
  3. private TelephonyManager tm;
  4. public ListenPhoneService() {
  5. }
  6.  
  7. @Override  
  8. public   void onCreate() {
  9. super .onCreate();
  10. mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
  11. tm=(TelephonyManager)getSystemService(Service.TELEPHONY_SERVICE);
  12. }
  13. @Override  
  14. public   int onStartCommand(Intent intent, int flags, int startId) {
  15. if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){ //Outgoing call broadcast, android has no incoming call broadcast  
  16. } else { //Remove the outgoing call status and it will be the incoming call status  
  17. //Method 1  
  18. //Get the incoming call number  
  19. // String number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);  
  20. //Get the phone status  
  21. // String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);  
  22. // Log.d("jereh", "incoming phone:" + number);  
  23. // Log.d("jereh","call state:"+state);  
  24. // TelephonyManager.EXTRA_STATE_IDLE: No incoming call or hung up  
  25. // TelephonyManagerEXTRA_STATE_OFFHOOK: pick up the call  
  26. // TelephonyManager.EXTRA_STATE_RINGING: When a call comes in, the ringing  
  27. // if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){  
  28. // if(number.equals("13280998858")){//Intercept the specified phone number  
  29. // mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);  
  30. // Log.d("jereh","The call was intercepted");  
  31. // stopCall();  
  32. // mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); //Restore ringtone  
  33. // }  
  34. // }else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){  
  35. //Pick up the phone  
  36. // recordCall(); //Start recording  
  37. // }else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){  
  38. // stopCall(); //Stop recording  
  39. // }  
  40. //Method 2  
  41. // Set up a listener to monitor the phone status  
  42. tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
  43. }
  44.  
  45. return   super .onStartCommand(intent, flags, startId);
  46. }
  47.  
  48. /**
  49. * Hang up the phone
  50. */  
  51. private   void stopCall(){
  52. try {
  53. //Android's design hides the ServiceManager, so it can only be obtained using the reflection mechanism.  
  54. Method method=Class.forName( "android.os.ServiceManager" ).getMethod( "getService" , String. class );
  55. IBinder binder = (IBinder) method.invoke ( null , new Object [] { "phone" }); // Get the system phone service  
  56. ITelephony telephoney=ITelephony.Stub.asInterface(binder);
  57. telephoney.endCall(); //Hang up the call  
  58. stopSelf(); //Stop service  
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. PhoneStateListener listener= new PhoneStateListener(){
  64. @Override  
  65. public   void onCallStateChanged( int state,String incomingNumber){
  66. switch (state) {
  67. //The phone is idle  
  68. case TelephonyManager.CALL_STATE_IDLE:
  69. stopCall(); //Stop recording  
  70. break ;
  71. //Pick up the phone  
  72. case TelephonyManager.CALL_STATE_OFFHOOK:
  73. recordCall(); //Start recording  
  74. break ;
  75. // When the bell rings  
  76. case TelephonyManager.CALL_STATE_RINGING:
  77. Log.e( "jereh" , "The incoming number is:" + incomingNumber);
  78. // If the number is in the blacklist  
  79. if (incomingNumber.equals( "123456" )) {
  80. // If it is a blacklist, block it  
  81. stopCall();
  82. }
  83. break ;
  84. }
  85. }
  86. };
  87.  
  88. /**
  89. * Stop recording
  90. */  
  91. private   void stopRecord(){
  92. if (recording){
  93. recorder.stop();
  94. recorder.release();
  95. recording = false ;
  96. stopSelf(); //Stop service  
  97. }
  98. }
  99. /**
  100. * Phone recording
  101. */  
  102. private MediaRecorder recorder;
  103. private   boolean recording ;
  104. private   void recordCall(){
  105. Log.d( "jereh" , "record calling" )
  106. if ( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  107. recorder= new MediaRecorder();
  108. recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //Read the sound from the microphone  
  109. recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //Set the output format  
  110. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // encoding method  
  111. File file= new File(Environment.getDownloadCacheDirectory().getAbsolutePath(), "recorder" );
  112. if (!file.exists()){
  113. file.mkdir();
  114. }
  115. recorder.setOutputFile(file.getAbsolutePath() + "/"  
  116. + System.currentTimeMillis() + "3gp" ); // The storage location is in the recorder directory of the sd card  
  117. try {
  118. recorder.prepare();
  119. recorder.start();
  120. recording = true ;
  121.  
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. }
  127. @Override  
  128. public IBinder onBind(Intent intent) {
  129. throw   new UnsupportedOperationException( "Not yet implemented" );
  130. }
  131. }
  132.  
  133. Copy code
  134.  
  135. Service XML Configuration
  136.  
  137. <service
  138. android:name= ".ListenPhoneService"  
  139. android:enabled= "true"  
  140. android:exported= "true" >
  141. </service>

5. Don’t forget to set some permissions

  1. <!-- Add permission to access mobile phone status -->
  2. <uses-permission android:name= "android.permission.READ_PHONE_STATE" />
  3. <!-- Make phone calls permission -->
  4. <uses-permission android:name= "android.permission.CALL_PHONE" />
  5. <!-- Permission to monitor outgoing calls from your mobile phone -->
  6. <uses-permission android:name= "android.permission.PROCESS_OUTGOING_CALLS" />
  7. <!-- Create and delete file permissions in SDCard -->
  8. <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  9. <!-- Permission to write data to SDCard -->
  10. <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />

<<:  What is the difference between a programmer and an engineer?

>>:  20 new tricks Facebook is using to eat up the Internet: Do you want to learn from them?

Recommend

New Android virus: Can root any phone and cannot be deleted

[[155228]] Beijing time, November 6th morning new...

A guide to popular beauty notes on Xiaohongshu!

The beauty category is Xiaohongshu’s traditional ...

Learn insurance sales from Ye Yunyan: 6 quick closing methods

Course Highlights 1. 23 years of practical sales ...

Was chocolate originally a drink? And you had to add chili to it!!

Do you remember the feeling of eating chocolate f...

Pinduoduo’s monthly card operating routine!

The latest version of Pinduoduo's money-savin...

How to manage your Android code with Gerrit?

Author: Hu Rui, Unit: China Mobile Smart Home Ope...

Lao Huqun's Getting Started Tutorial on Buying from 0 to 1

Taoke Park Lao Hu and major team leaders teach yo...

The inside story of Android creator's farewell to Google

Who is Andy Rubin? He is a great man who named th...

Can avocados reduce belly fat? 150 overweight people tell you the answer

The surface is bumpy and it is neither sweet nor ...

Analysis of Huaxizi and Winona’s private domain marketing!

Private domain marketing is not a new term, but p...

The person who discovered the most chemical elements in history is a poet

Humphry Davy was not only a romantic poet, but al...