您的位置:首页 > 其它

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 下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: