最近做了一个监听电话并录音的小例子,而保证它在后台运行不被杀确实不容易。

首先在主service中:

service中重写onStartCommand方法,这个方法有三个返回值, START_STICKY是service被kill掉后自动

public int onStartCommand(Intent intent, int flags, int startId) {IntentFilter filter=new IntentFilter();filter.addAction(Intent.ACTION_TIME_TICK);UserPresentReceive userReceive = new UserPresentReceive();registerReceiver(userReceive,filter);int flag = START_STICKY;// 判断是否是Broadcast发来的intentif (!("Protect".equals(intent.getAction()))) {System.out.println("action" + intent.getAction());this.outGoingNum = intent.getStringExtra("outGoingNums");// 将耗时操作放到子线程中
            backGroundExecution();}return super.onStartCommand(intent, flag, startId);}

在onDestroy()方法中,启动当前服务,同时再启动protectService()

public void onDestroy() {System.out.println("ServicePhone_onDestroy");Intent ins = new Intent(this, ServicePhone.class);ins.setAction("Protect");startService(ins);Intent in = new Intent(this, ProtectService.class);startService(in);super.onDestroy();}

新建一个ProtectService服务 ,同时再建一个broadcastReceive

public int onStartCommand(Intent intent, int flags, int startId) {Intent ins = new Intent(this, ServicePhone.class);ins.setAction("Protect");startService(ins);flags = START_STICKY;return super.onStartCommand(intent, flags, startId);}public void onDestroy() {Intent in = new Intent(this, ServicePhone.class);in.setAction("Protect");startService(in);//相互关联Intent ins = new Intent(this, ProtectService.class);startService(ins);super.onDestroy();}

ProtectService中用alarmManager来不断触发UserPresentReceive

private void proctectServiceInBack() {/*** 指定时间间隔 重复唤醒alarm*/AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);// 在合适的时间醒来int alarmType = AlarmManager.RTC_WAKEUP;// 设置时间长度long timeOrLengthofWait = 150;long timeOrLengthofWaits=20000;Intent intent = new Intent(getApplicationContext(),UserPresentReceive.class);intent.setAction("protect");PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0,intent, 0);alarmManager.setRepeating(alarmType, timeOrLengthofWait,timeOrLengthofWaits, alarmIntent);}

在UserPresentReceive 监听系统广播:

Intent.ACTION_TIME_TICK,这个广播每分钟发送一次,我们可以每分钟检查一次Service的运行状态,如果已经被结束了,就重新启动Service。
同时监听    ACTION_BOOT_COMPLETED

    public void onReceive(Context content, Intent intent) {System.out.println("UserPresentReceive");if(Intent.ACTION_TIME_TICK.equals(intent.getAction()) ){Intent in=new Intent(content, ProtectService.class);content.startService(in);}else if(Intent.ACTION_BUG_REPORT.equals(intent.getAction())){Intent in=new Intent(content, ProtectService.class);content.startService(in);}else{Intent in=new Intent(content, ProtectService.class);content.startService(in);}//开机自启if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){Intent in=new Intent(content,ServicePhone.class);in.setAction("Protect");content.startService(in);} else{Intent in=new Intent(content, ServicePhone.class);in.setAction("Protect");content.startService(in);}}}

另外建再建一个broadcastReceive来监听电话状态

if (Intent.ACTION_NEW_OUTGOING_CALL.equals(intent.getAction())) {//取得号码String outGoingNum = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);Intent in=new Intent(content,ServicePhone.class);in.setAction(".b2");//将号码传给servicein.putExtra("outGoingNums", outGoingNum);content.startService(in);} else{Intent in=new Intent(content, ServicePhone.class);in.setAction(".b3");content.startService(in);}}

manifest.xml    application中可以加  android:persistent="true"  提高不被杀死的几率

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:persistent="true"android:theme="@style/AppTheme" >

将protectService另起一个进程

<service   android:process=":protect"android:name="com.example.phonelisten.ProtectService"android:enabled="true" ></service>

监听系统广播  设置android:priority 提高优先权

<receiver android:name="com.example.phonelisten.ReceivePhone" ><intent-filter android:priority="1000" ><action android:name="android.intent.action.TIME_TICK" /><action android:name="android.intent.action.PHONE_STATE" /><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.intent.action.NEW_OUTGOING_CALL" /></intent-filter></receiver><receiver android:name="com.example.phonelisten.UserPresentReceive" ><intent-filter android:priority="999" ><action android:name="android.intent.action.BOOT_COMPLETED" /><action android:name="android.intent.action.TIME_TICK" /><action android:name="android.intent.action.BUG_REPORT" /><action android:name="android.intent.action.USER_PRESENT" /><action android:name="android.media.RINGER_MODE_CHANGED" /></intent-filter></receiver>

转载于:https://www.cnblogs.com/mydomainlistentome/p/4692795.html

让后台服务不被杀———电话录音相关推荐

  1. Android电话录音

     Android电话录音 本例是一个通用的电话录音方案,先创建一个广播接收, package zhangphil.phonerecorder;import android.content.Broa ...

  2. android service 样例(电话录音和获取系统当前时间)

    关于android service 的具体解释请參考: android四大组件--android service具体解释.以下将用两个实例具体呈现Android Service的两种实现. 一个是st ...

  3. Android 杀不掉的后台服务的一种实现

    有些应用程序需要后台的常驻服务,但没有任何处理时服务会被系统不定时的杀掉,特别是在内存不足时.同时服务也极容易被第三方软件释放内存时清理掉,今天同过一种实现方式来解决上述问题. 在上代码前,我们应该简 ...

  4. Android后台服务Service

    后台服务 桌面应用程序:可见 服务:不可见  长期在后台运行 帮助应用执行耗时的操作 安卓的服务:安卓四大组件之一  不可见   后台长期运行 界面与服务有时候要执行数据交互 如何创建服务: 1. 创 ...

  5. 71.android 简单的电话录音并保存到本地(来电和去电都支持)

    //第一步 先加权限 在AndroidManifest.xml里: //有打电话的权限,读写权限,还有录音权限. <uses-permission android:name="andr ...

  6. python打电话播放语音_让电话录音也能看得见,录音一键转为文字

    最近看到有朋友在问手机通话怎么录音?他说他经常需要和领导进行电话会议,每次通话时间都很长,使用通话录音就能将内容记录下来,这样就不会遗忘重要内容了.如果你拥有一个一打电话就仿佛吃了脉动停不下来的领导, ...

  7. 宝元系统通讯软件recon_优软科技会场式电话录音系统

    电话录音系统用于实时地监控和记录电话通讯信息,包括电话号码.呼叫时间.通话内容等.有着监督服务质量.营销分析,和解决纠纷等重要作用. 优软科技会场式录音 1 基本功能:提供基本的录音.监听.查询.管理 ...

  8. 八百呼电话录音系统--让通讯管理更安心

    只要是每天需要呼入呼出电话的企业,都建议安装电话录音系统录音电话.因为电话录音系统能够方便这些企业进行管理,无论有多少站点,都能统一管理和分配任务,并且使用电话录音系统还能够监管通话质量 八百呼电话录 ...

  9. android电话、短信黑白名单拦截、电话录音

    功能描述: 总的来说这是一个防骚扰的应用,设置黑名单,白名单,通话录音名单.添加到黑名单的联系人或号码将被拒绝来电或短信:添加到白名单的联系人或号码将通过来电或短信(除白名单以外的号码将被拒绝来电或短 ...

最新文章

  1. linux python保存mp4
  2. 90后,一个即将成为程序员的我
  3. 使用malloc初始化一个类和new初始化一个类的区别
  4. gradle的多项目构建(九)
  5. java获取ip地址不重复,如何在android(Java)中验证Ip地址[重复]
  6. java maven调用hbase
  7. PHP trim()函数详解
  8. caffe boost cuda __float128 undefined
  9. Java案例:Java版生命游戏
  10. c语言解三元一次方程组_在R里面对三元一次方程求解
  11. Python ICMP扫描
  12. HTML+CSS基础课程 笔记
  13. FAT32文件系统格式详解(图文针对具体文件存储,分析fat32 SD卡)
  14. 解决base64解码乱码问题
  15. linux各种命令手册
  16. [iOS]分析Mach-O文件
  17. php+html文本域,html的文本域和表单域
  18. logback各标签详解
  19. 插值中产生的龙格现象
  20. openwrt RK3568_EVB移植

热门文章

  1. 性能优化指南(5000 字小结)
  2. 记一次内存泄漏问题的排查经历
  3. 许式伟:毕业两年成为首席架构师,我的技术学习方法论
  4. Java 应用中的日志
  5. Spring和SpringMVC的总结
  6. Java 洛谷 P1319 压缩技术
  7. jQuery-事件委托(基本概述+实例)
  8. 计算机启动操作系统的过程,操作系统启动过程
  9. java 字节缓冲_Java字节缓冲流原理与用法详解
  10. linux 网卡丢弃多播包,rp_filter及Linux下多网卡接收多播的问题