[[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
- public class TelReceiver extends BroadcastReceiver {
- public TelReceiver() {
- }
-
- @Override
- public void onReceive(Context context, Intent intent) {
- Intent i= new Intent(context,ListenPhoneService. class );
- i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- i.setAction(intent.getAction());
- i.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER,
- intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER));
- i.putExtra(TelephonyManager.EXTRA_STATE,
- intent.getStringExtra(TelephonyManager.EXTRA_STATE));
- context.startService(i);
- }
- }
Register broadcast: - <receiver android:name= ".TelReceiver" >
- <intent-filter android:priority= "1000" >
- <action android:name= "android.intent.action.PHONE_STATE" />
- <action android:name= "android.intent.action.NEW_OUTGOING_CALL" />
- </intent-filter>
- </receiver>
4. Write the ListenPhoneService component
- public class ListenPhoneService extends Service {
- private AudioManager mAudioManager;
- private TelephonyManager tm;
- public ListenPhoneService() {
- }
-
- @Override
- public void onCreate() {
- super .onCreate();
- mAudioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
- tm=(TelephonyManager)getSystemService(Service.TELEPHONY_SERVICE);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
- } else {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
- }
-
- return super .onStartCommand(intent, flags, startId);
- }
-
-
-
-
- private void stopCall(){
- try {
-
- Method method=Class.forName( "android.os.ServiceManager" ).getMethod( "getService" , String. class );
- IBinder binder = (IBinder) method.invoke ( null , new Object [] { "phone" });
- ITelephony telephoney=ITelephony.Stub.asInterface(binder);
- telephoney.endCall();
- stopSelf();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- PhoneStateListener listener= new PhoneStateListener(){
- @Override
- public void onCallStateChanged( int state,String incomingNumber){
- switch (state) {
-
- case TelephonyManager.CALL_STATE_IDLE:
- stopCall();
- break ;
-
- case TelephonyManager.CALL_STATE_OFFHOOK:
- recordCall();
- break ;
-
- case TelephonyManager.CALL_STATE_RINGING:
- Log.e( "jereh" , "The incoming number is:" + incomingNumber);
-
- if (incomingNumber.equals( "123456" )) {
-
- stopCall();
- }
- break ;
- }
- }
- };
-
-
-
-
- private void stopRecord(){
- if (recording){
- recorder.stop();
- recorder.release();
- recording = false ;
- stopSelf();
- }
- }
-
-
-
- private MediaRecorder recorder;
- private boolean recording ;
- private void recordCall(){
- Log.d( "jereh" , "record calling" )
- if ( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- recorder= new MediaRecorder();
- recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
- recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- File file= new File(Environment.getDownloadCacheDirectory().getAbsolutePath(), "recorder" );
- if (!file.exists()){
- file.mkdir();
- }
- recorder.setOutputFile(file.getAbsolutePath() + "/"
- + System.currentTimeMillis() + "3gp" );
- try {
- recorder.prepare();
- recorder.start();
- recording = true ;
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- throw new UnsupportedOperationException( "Not yet implemented" );
- }
- }
-
- Copy code
-
- Service XML Configuration
-
- <service
- android:name= ".ListenPhoneService"
- android:enabled= "true"
- android:exported= "true" >
- </service>
5. Don’t forget to set some permissions - <!-- Add permission to access mobile phone status -->
- <uses-permission android:name= "android.permission.READ_PHONE_STATE" />
- <!-- Make phone calls permission -->
- <uses-permission android:name= "android.permission.CALL_PHONE" />
- <!-- Permission to monitor outgoing calls from your mobile phone -->
- <uses-permission android:name= "android.permission.PROCESS_OUTGOING_CALLS" />
- <!-- Create and delete file permissions in SDCard -->
- <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <!-- Permission to write data to SDCard -->
- <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
|