標籤:
首先來點的時候一共有三個狀態, 空閑,響鈴,摘機(接電話)
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
許可權分別是對電話操作,錄音許可權,sd卡許可權
想要開機啟動服務就可以用廣播接受者來做。
package com.example.luyin;import android.app.Service;import android.content.Intent;import android.media.MediaRecorder;import android.os.IBinder;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.widget.Toast;public class MyService extends Service { private MediaRecorder recorder; public MyService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { Toast.makeText(MyService.this, "服務啟動", 1).show(); // TODO Auto-generated method stub super.onCreate(); // 獲得電話管理器,用來監聽電話的狀態 TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // event設定電話只監聽電話狀態改變 manager.listen(new Listen(), PhoneStateListener.LISTEN_CALL_STATE); } private class Listen extends PhoneStateListener { // 電話狀態一旦改變調用 // 通過state的傳回值來確定有沒有來電話 @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); switch (state) { // 空閑 case TelephonyManager.CALL_STATE_IDLE: Toast.makeText(MyService.this, "空閑", 1).show(); if(recorder!=null){ //錄音結束並且釋放資源 recorder.stop(); recorder.release(); recorder=null; } break; // 響鈴 case TelephonyManager.CALL_STATE_RINGING: Toast.makeText(MyService.this, "響鈴", 1).show(); if (recorder == null) { // 錄音機 recorder = new MediaRecorder(); // 設定錄音的來源是麥克風 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 設定錄音的儲存格式 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 設定儲存路徑 recorder.setOutputFile("sdcard/voice.3gp"); // 設定編碼 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); // 準備錄音 try { recorder.prepare(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } break; // 接聽 case TelephonyManager.CALL_STATE_OFFHOOK: Toast.makeText(MyService.this, "摘機", 1).show(); if(recorder!=null){ //開始錄音 recorder.start(); } break; } } }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.luyin" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.luyin.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.example.luyin.MyService" > </service> </application></manifest>
android四大組件之Service 電話錄音