Android–service服务–MediaRecorder–电话录音

一、通过Intent启动服务:

 

 

[java][/java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. package com.jiangge.callstatuslistener;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.view.Menu;
  6. public class MainActivity extends Activity {
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main);
  11.         Intent intent = new Intent(this, PhoneStatusService.class);
  12.         startService(intent);
  13.     }
  14. }

 

二、PhoneStatusService.java

 

[java][/java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. package com.jiangge.callstatuslistener;
  2. import java.io.IOException;
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.media.MediaRecorder;
  6. import android.os.IBinder;
  7. import android.telephony.PhoneStateListener;
  8. import android.telephony.TelephonyManager;
  9. public class PhoneStatusService extends Service {
  10.     @Override
  11.     public IBinder onBind(Intent intent) {
  12.         return null;
  13.     }
  14.     @Override
  15.     public void onCreate() {
  16.         super.onCreate();
  17.         System.out.println(“===>>>服务被创建了”);
  18.         TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  19.         tm.listen(new MyPhoneStatusListener(), PhoneStateListener.LISTEN_CALL_STATE);// 监听手机的通话状态的变化
  20.     }
  21.     private class MyPhoneStatusListener extends PhoneStateListener {
  22.         MediaRecorder recorder;
  23.         @Override
  24.         public void onCallStateChanged(int state, String incomingNumber) {
  25.             super.onCallStateChanged(state, incomingNumber);
  26.             try {
  27.                 switch (state) {
  28.                 case TelephonyManager.CALL_STATE_IDLE: // 空闲状态,没有通话没有响铃
  29.                     if (recorder != null) {
  30.                         recorder.stop();
  31.                         recorder.reset();
  32.                         recorder.release();
  33.                         recorder = null;
  34.                     }
  35.                     break;
  36.                 case TelephonyManager.CALL_STATE_RINGING: // 响铃状态
  37.                     System.out.println(“发现来电号码” + incomingNumber);
  38.                     recorder = new MediaRecorder();// 创建一个录音机
  39.                     recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 设置录制的音频源从话筒里面获取声音
  40.                     recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  41.                     recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  42.                     recorder.setOutputFile(“/sdcard/” + System.currentTimeMillis() + “.3gp”);
  43.                     recorder.prepare();
  44.                     break;
  45.                 case TelephonyManager.CALL_STATE_OFFHOOK: // 通话状态
  46.                     if (recorder != null) {
  47.                         recorder.start();
  48.                     }
  49.                     break;
  50.                 }
  51.             } catch (Exception e) {
  52.                 e.printStackTrace();
  53.             }
  54.         }
  55.     }
  56.     @Override
  57.     public void onDestroy() {
  58.         super.onDestroy();
  59.         System.out.println(“===>>>服务被销毁了”);
  60.     }
  61. }

注:/sdcard 不要硬编码

 

Do not hardcode “/sdcard/”; use Environment.getExternalStorageDirectory().getPath()instead

 

三、清单文件:

 

[html][/html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <?xml version=”1.0″ encoding=”utf-8″?>
  2. <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
  3.     package=”com.jiangge.callstatuslistener”
  4.     android:versionCode=”1″
  5.     android:versionName=”1.0″ >
  6.     <uses-sdk
  7.         android:minSdkVersion=”8″
  8.         android:targetSdkVersion=”17″ />
  9.     <uses-permission android:name=”android.permission.READ_PHONE_STATE”/>
  10.     <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
  11.     <uses-permission android:name=”android.permission.RECORD_AUDIO”/>
  12.     <application
  13.         android:allowBackup=”true”
  14.         android:icon=”@drawable/ic_launcher”
  15.         android:label=”@string/app_name”
  16.         android:theme=”@style/AppTheme” >
  17.         <activity
  18.             android:name=”com.jiangge.callstatuslistener.MainActivity”
  19.             android:label=”@string/app_name” >
  20.             <intent-filter>
  21.                 <action android:name=”android.intent.action.MAIN” />
  22.                 <category android:name=”android.intent.category.LAUNCHER” />
  23.             </intent-filter>
  24.         </activity>
  25.         <service android:name=”.PhoneStatusService” >
  26.         </service>
  27.     </application>
  28. </manifest>

注,权限:

 

 

[html][/html] view plaincopy在CODE上查看代码片派生到我的代码片

  1. <uses-permission android:name=”android.permission.READ_PHONE_STATE”/>
  2. <uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
  3. <uses-permission android:name=”android.permission.RECORD_AUDIO”/>

 

效果图:

项目文件结构:

 

注:MediaRecorder文档中写得很清楚的。

标签