标签:android style class blog code java
步骤:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.callstatelistener" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <!-- 监听电话状态 --> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- 音频录制的权限 --> <uses-permission android:name="android.permission.RECORD_AUDIO"/> <!-- 向SD卡读写的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_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.callstatelistener.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 --> <service android:name="com.example.callstatelistener.CallStateService"></service> </application> </manifest>
CallStateService.java
package com.example.callstatelistener; import java.io.File; import java.io.IOException; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.MediaRecorder; import android.os.Environment; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; /** * 监听电话状态service * * @author zhaoyazhi * * 2014-6-17 */ public class CallStateService extends Service { public static final String TAG = "aaa"; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { super.onCreate(); // 取得电话服务 TelephonyManager telManager = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); // 电话监听的对象,电话监听的行为 telManager.listen(new MyPhoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE); } class MyPhoneStateListener extends PhoneStateListener { private MediaRecorder recorder; /** * int state电话状态。 String incomingNumber电话号码 */ @Override public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); switch (state) { case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */ if (recorder != null) { recorder.stop();// 停止刻录 recorder.reset(); // 重设 recorder.release(); // 刻录完成一定要释放资源 } Log.i(TAG, "------------------没有电话来"); break; case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */ Log.i(TAG, "------------------来电话了---并且响铃"); recorder = new MediaRecorder(); recorder.setAudioSource(MediaRecorder.AudioSource.MIC);// 从麦克风采集声音 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 内容输出格式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 音频编码方式 // 获取sd卡目录 File dir = Environment.getExternalStorageDirectory(); recorder.setOutputFile(dir.getPath()+ "/csdn.amr"); try { recorder.prepare(); // 预期准备 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */ Log.i(TAG, "------------------通话中"); if (recorder != null) { recorder.start(); // 开始刻录 } break; default: break; } }; } }
查看sd卡文件
赵雅智_service电话监听2加接通电话录音,布布扣,bubuko.com
标签:android style class blog code java
原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/31788115