import java.text.SimpleDateFormat;import java.util.Date;import com.app.utils.NotificationUtil;import android.app.Service;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.media.MediaRecorder;import android.os.Environment;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;import android.widget.Toast;public class PhoneListenerService extends Service {private static final String SDPATH = Environment .getExternalStorageDirectory() + "/";private static final String TAG = "PhoneListenerService";private int notification_startid = 1;// 播出电话 开始录音 notification IDprivate SharedPreferences settings;private boolean isRecord ;@Overridepublic IBinder onBind(Intent intent) { return null;}@Overridepublic void onCreate() { super.onCreate(); TelephonyManager telephonyManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); PhoneStateListener stateListener = new PhoneStateListener() { private MediaRecorder recorder; private String incomingNumber; @Override public void onCallStateChanged(int state, String incomingNumber) { Log.v(TAG, "state:" + state); Log.v(TAG, "incomingNumber:" + incomingNumber); settings = getSharedPreferences("settings", 0); // 获取SharedPreferences中保存的信息 isRecord = settings.getBoolean("isRecord", false); switch (state) { case TelephonyManager.CALL_STATE_IDLE:// 无电话 Log.v(TAG, "No call"); Toast.makeText( PhoneListenerService.this, "state:" + TelephonyManager.CALL_STATE_IDLE + ",暂无电话", Toast.LENGTH_LONG).show(); if (recorder == null) break; if (recorder != null && isRecord ) { NotificationUtil notificationUtilstop = new NotificationUtil( PhoneListenerService.this, notification_startid); notificationUtilstop.showNotification(R.drawable.icon, "Stop Record ", "RecordMyCall", "通话录音结束"); notificationUtilstop .cancelNotification(notification_startid); this.incomingNumber = null; recorder.stop(); // recorder.release(); recorder = null; } break; case TelephonyManager.CALL_STATE_OFFHOOK: // 接电话 Log.v(TAG, "start record"); Toast.makeText( PhoneListenerService.this, "state:" + TelephonyManager.CALL_STATE_OFFHOOK + ",正在接通电话!", Toast.LENGTH_LONG).show(); Log.v(TAG, "isRecord:" + isRecord); if (recorder == null) { this.incomingNumber = "from" + incomingNumber; recorder = new MediaRecorder(); } if (isRecord) { NotificationUtil notificationUtilstart = new NotificationUtil( PhoneListenerService.this, notification_startid); notificationUtilstart.showNotification(R.drawable.icon, "Start Record ", "RecordMyCall", "通话录音开始"); try {