首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

Service范例应用之电话录音

2012-11-12 
Service实例应用之电话录音我们知道,Android有四大组件,分别是Activity,Content Provider , Broadcast Rec

Service实例应用之电话录音

我们知道,Android有四大组件,分别是Activity,Content Provider , Broadcast Receiver 和今天我们要用到的Service。

我们说Service是看不见摸不着的,是因为它对用户是不可见的,Service作为四大组件之一,它的职责是工作在后台,处理

一些比较耗时的操作(如下载,播放媒体文件,检测版本更新和日志输出等)。

就像学习Activity组件一样,要想熟悉Service,必须知道它的整个工作流程,即它的生命周期(Life-cycle),下面是官网给

出的一个具体图例:

Service范例应用之电话录音


从图例我们可以知道,Service有两种启动方式,第一种是调用Context.startService()方法,

另外一种是调用Context.bindService().

对于第一种调用方法,系统会先调用onCreate()方法,负责初始化工作,然后调用onStart()方法,

启动Service,如果程序显示调用Context.stopService()方法或者Service自身调用stopSelf()方法

或者stopSelfResult()方法,都会调用onDestroy()方法,结束该服务,另外需要注意的是,调用者

和Service是没有关联的,也就是说,如果调用者退出了,并不会影响服务的运行。

对于第二种方式,调用方法是Context.bindService(),系统会先调用onCreate()方法,然后调用onBind()

方法,和调用者绑定,如果调用者退出,服务会调用onDestroy()方法结束自己的运行。


学完了一个东西,如果趁热打铁,自己编写一个demo,肯定会加深对该知识点的理解。

废话少说,下面给出一个电话录音的例子:


首先,也是最重要的,关于权限的声明:

        <receiver android:name=".MyBroadcastReceiver" >            <intent-filter>                <action android:name="android.intent.action.BOOT_COMPLETED" />                <action android:name="zjut.tsw.receiver" />            </intent-filter>        </receiver>        <service android:name=".PhoneListenerService" >        </service>

这样,当您打开了该软件,或者开机后,该服务会自动启动并运行(你可以在Android机上按Menu键->设置->应用程序->运行的服务 查看该服务的具体状态)

当有人打电话给你或者你打给别人,电话录音会保存在/recorded_calls/...里面。(该程序含有潜在的错误,如果通话时间足够多或足够长(煲电话粥的同志们注意了哦),可能会导致SD卡内存不足)


有需要源码的请Click:http://download.csdn.net/detail/czjuttsw/4700525


Over...





2楼zx012345昨天 16:39
请问楼主:MediaRecorder.AudioSource.VOICE_CALL 这个参数有效吗?
Re: czjuttsw昨天 17:35
回复zx012345n当然有啊,你看源码就知道了:n public final class AudioSource {n * in include/media/mediarecorder.h!n */n private AudioSource() {}n public static final int DEFAULT = 0;n /** Microphone audio source */n public static final int MIC = 1;nn /** Voice call uplink (Tx) audio source */n public static final int VOICE_UPLINK = 2;nn /** Voice call downlink (Rx) audio source */n public static final int VOICE_DOWNLINK = 3;nn /** Voice call uplink + downlink audio source */n public static final int VOICE_CALL = 4;nn /** Microphone audio source with same orientation as camera if available, the mainn * device microphone otherwise */n public static final int CAMCORDER = 5;nn /** Microphone audio source tuned for voice recognition if available, behaves liken * {@link #DEFAULT} otherwise. */n public static final int VOICE_RECOGNITION = 6;n }
1楼zx012345昨天 09:36
有这个参数,但并不代表它就有效。n我测试过几部手机,没有一部能用MediaRecorder.AudioSource.VOICE_CALL进行电话录音。
Re: czjuttsw昨天 11:46
回复zx012345n我的手机就有效呢。。

热点排行