您的位置:首页 > 移动开发 > Android开发

android四大组件之Service 电话录音

2015-11-16 20:03 417 查看
首先来点的时候一共有三个状态, 空闲,响铃,摘机(接电话)

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

权限分别是对电话操作,录音权限,sd卡权限

想要开机启动服务就可以用广播接受者来做。

package com.example.luyin;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MyService extends Service {
private MediaRecorder recorder;

public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
Toast.makeText(MyService.this, "服务启动", 1).show();
// TODO Auto-generated method stub
super.onCreate();
// 获得电话管理器,用来监听电话的状态
TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// event设置电话只监听电话状态改变
manager.listen(new Listen(), PhoneStateListener.LISTEN_CALL_STATE);
}

private class Listen extends PhoneStateListener {
// 电话状态一旦改变调用
// 通过state的返回值来确定有没有来电话
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
switch (state) {
// 空闲
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(MyService.this, "空闲", 1).show();
if(recorder!=null){
//录音结束并且释放资源
recorder.stop();
recorder.release();
recorder=null;
}
break;
// 响铃
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(MyService.this, "响铃", 1).show();
if (recorder == null) {
// 录音机
recorder = new MediaRecorder();
// 设置录音的来源是麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置录音的保存格式
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置保存路径
recorder.setOutputFile("sdcard/voice.3gp");
// 设置编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 准备录音
try {
recorder.prepare();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
// 接听
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(MyService.this, "摘机", 1).show();
if(recorder!=null){
//开始录音
recorder.start();
}
break;
}
}
}
}


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.luyin"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_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.luyin.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="com.example.luyin.MyService" >
</service>
</application>

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