016_01Service的应用--拨打电话自动录音
2015-05-26 21:43
169 查看
package com.example.day16_03phonerecorder; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, MyPhoneRecordService.class); startService(intent); } }
package com.example.day16_03phonerecorder; import java.io.IOException; import android.app.Service; import android.content.Intent; import android.media.MediaRecorder; import android.media.MediaRecorder.AudioEncoder; import android.media.MediaRecorder.AudioSource; import android.media.MediaRecorder.OutputFormat; import android.os.IBinder; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; public class MyPhoneRecordService extends Service { MediaRecorder mMediaRecorder; boolean recordflag =false; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("MyPhoneRecordService.onCreate()"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub System.out.println("MyPhoneRecordService.onStartCommand()"); //服务里要监听电话的状态 // 1,如果发现电话接通--》就是开始录音 // 2.如果发现电话挂断 --》就停止录音 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); telephonyManager.listen(new MyphoneStateListener(), PhoneStateListener.LISTEN_CALL_STATE); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); System.out.println("MyPhoneRecordService.onDestroy()"); } class MyphoneStateListener extends PhoneStateListener{ /* *//** Device call state: No activity. *//* public static final int CALL_STATE_IDLE = 0; *//** Device call state: Ringing. A new call arrived and is * ringing or waiting. In the latter case, another call is * already active. *//* public static final int CALL_STATE_RINGING = 1; *//** Device call state: Off-hook. At least one call exists * that is dialing, active, or on hold, and no calls are ringing * or waiting. *//* public static final int CALL_STATE_OFFHOOK = 2;*/ @Override public void onCallStateChanged(int state, String incomingNumber) { // TODO Auto-generated method stub super.onCallStateChanged(state, incomingNumber); switch (state) { case 0: if (recordflag) { //停止录音 // 8. 停止录音 mMediaRecorder.stop(); // 9. 释放资源 mMediaRecorder.release(); recordflag=false; } System.out .println("MyPhoneRecordService.MyphoneStateListener.onCallStateChanged()电话挂断"); break; case 1: System.out .println("MyPhoneRecordService.MyphoneStateListener.onCallStateChanged()正在响铃"); break; case 2: //开始录音 // 1. 创建一个MediaRecorder对象 mMediaRecorder = new MediaRecorder(); // 2. 设置音频源 mMediaRecorder.setAudioSource(AudioSource.MIC); // 3. 设置输出格式 mMediaRecorder.setOutputFormat(OutputFormat.THREE_GPP); // 4. 设置输出文件的名字 mMediaRecorder.setOutputFile(getFilesDir()+"/record.3pg"); // 5. 设置音频编码 mMediaRecorder.setAudioEncoder(AudioEncoder.DEFAULT); // 6. 准备以下, 马上开始录音 try { mMediaRecorder.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 7. 开始录音 mMediaRecorder.start(); // 录音开始了... recordflag=true; System.out .println("MyPhoneRecordService.MyphoneStateListener.onCallStateChanged()正在通话"); break; default: break; } } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.day16_03phonerecorder" 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"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".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=".MyPhoneRecordService"></service> </application> </manifest>
录音文件保存在/data/data/com.example.day16_03phonerecorder 下
相关文章推荐
- Android 小应用:“自动拨打电话app”制作心得
- Service实例应用之电话录音
- Service实例应用之电话录音
- [应用代码] android 自动接听电话和挂断 (适合目前所有版本)
- Andoid自动判断输入是电话,网址或者Email的方法----Linkify的应用!
- Android四大组件应用系列——实现电话拦截和电话录音
- iOS 程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
- 手机端网页点击链接触发自动拨打或保存电话的示例代码
- iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
- iOS应用调用手机发送短信和拨打电话
- iPhone 应用中实现拨打电话功能的代码
- Android使用am命令实现拨打电话、打开应用
- iOS应用拨打电话
- iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
- iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
- .Net语言 APP开发平台——Smobiler学习日志:在手机应用开发中如何快速调用电话拨打功能
- android service 例子(电话录音和获取系统当前时间)
- 赵雅智_service电话监听2加接通电话录音
- iOS程序中调用系统自带应用(短信,邮件,浏览器,地图,appstore,拨打电话)
- Android--service服务--MediaRecorder--电话录音