在清單文件中添加以下配置java
<!-- 配置廣播接收者 --> <receiver android:name="com.istarry.ipbohao.IpCallReceiver" > <intent-filter> <!-- 指定接收那個廣播 --> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> <!-- <data android:scheme=" " /> --> </intent-filter> </receiver>
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter intentFilter = new IntentFilter("android.intent.action.MEDIA_UNMOUNTED"); intentFilter.addDataScheme("file"); registerReceiver(new IpCallReceiver(), intentFilter); } }
需求:android
1.攔截廣播安全
<!-- 指定接收(攔截)那個廣播 --> <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
2.須要的權限app
<uses-permission android:name="android.permission.PROCESSOUTGOINGCALLS" />ide
3.建立一個類IpCallReceiver繼承BroadCastReceiver編碼
public class IpCallReceiver extends BroadcastReceiver { //當收到廣播時,此方法調用 @Override public void onReceive(Context arg0, Intent arg1) { //添加17951線路 //1.拿到用戶撥打的號碼 //廣播會發送一個Intent,攜帶數據 String number = getResultData(); //在號碼前加上17951,並返回數據 setResultData("17951"+number); } }
4.在清單文件中註冊廣播接收者線程
<application …… > ………… <!-- 配置廣播接收者 --> <receiver android:name="com.istarry.ipbohao.IpCallReceiver" > <!-- android:priority="1000"表明着給當前接收者設置優先級,優先級越高就越先接收到廣播,取值-1000~1000 --> <intent-filter android:priority="1000"> <!-- 指定接收那個廣播 --> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> <!-- <data android:scheme=" " /> --> </intent-filter> </receiver> …………
進程優先級由高到低,依次爲:code
Androidorm
TelephonyManager,設置PhoneStateListener監聽狀態繼承
權限
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" />配置清單文件
<service android:name="com.istarry.luyinji.LuyinjiService" />建立一個類LuyinjiService繼承Service
public class LuyinjiService extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); // 獲取電話管理器 TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); // 設置偵聽 // 參數2:設置監聽器只監聽什麼數據 tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE); } private MediaRecorder recorder; class MyListener extends PhoneStateListener { // 電話狀態改變時調用此方法 @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: System.out.println("空閒");
if (recorder != null) { // 中止錄音 recorder.stop(); // 釋放佔用的資源 recorder.release(); recorder = null; } break; case TelephonyManager.CALL_STATE_RINGING: System.out.println("響鈴"); if (recorder == null) { recorder = new MediaRecorder(); // 設置錄音源 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 設置錄音格式3gp格式 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // 設置錄音文件保存位置 recorder.setOutputFile("sdcard/voice.3gp"); // 設置音頻編碼(一種格式可能有多重種編碼方式) recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); try { // 準備完畢,隨時能夠錄音 recorder.prepare(); } catch (Exception e) { e.printStackTrace(); } } break; case TelephonyManager.CALL_STATE_OFFHOOK: System.out.println("摘機"); if (recorder != null) { recorder.start(); } break; } } } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub return super.onStartCommand(intent, flags, startId); } }