让后台服务不被杀———电话录音

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

首先在主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");
            //将号码传给service
            in.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"  提高不被杀死的概率

<application
        android: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>
时间: 2024-07-28 13:01:02

让后台服务不被杀———电话录音的相关文章

android如何做到类似于微信那样后台服务不会被杀死?

问题描述 正在做一款锁屏应用. 做锁屏肯定用到了service,可是我发现每日手动点击自带的内存清理按钮的时候,我的那个service总是会被杀死. 而微信的后台服务却是一直正常的运行,不会被杀掉. 360的话也不会被杀死,但是360会重新启动.而且360的是两个后台服务,我猜有可能会相互作用的,杀死一个的时候另一个接收到广播把其重启. 尝试过用startForeground以及提高service优先级的方式,发现都不行,service都会被杀死. 反编译了一下微信的代码. 请大家帮忙想想办法吧

赵雅智_service电话监听2加接通电话录音

步骤: 创建CallStateService继承Service 取得电话服务 监听电话动作 电话监听的对象 没有电话时 停止刻录 重设 刻录完成一定要释放资源 电话响铃时 从麦克风采集声音 内容输出格式 音频编码方式 获取sd卡目录并存入 电话接通时 开始录制 电话监听的行为 启动service 注册service并添加监听电话状态的权限 监听电话状态权限 sd卡读写权限 音频录制权限 AndroidManifest.xml <?xml version="1.0" encodin

电话状态、电话录音

获得电话状态需要在请电脑文件中添加如下权限 <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 监听电话的步骤: 说用到主要方法: void listen(PhoneStateListener listener, int events) Registers a listener object to receive notification of changes in specified

Android四大组件应用系列——实现电话拦截和电话录音

一.问题描述 使用BordercastReceiver和Service组件实现下述功能: 1.当手机处于来电状态,启动监听服务,对来电进行监听录音. 2.设置电话黑名单,当来电是黑名单电话,则直接挂断. 当拨打电话或电话状态发生改变时,系统就会发出有序广播,因此我们可以使用BordercastReceiver接受广播,因BordercastReceiver执行时间短不能执行耗时任务也不能使用子线程,因此我们应启动一个Service来监听电话并进行处理 二.加入AIDL文件 Android没有对外

Android实战简易教程-第五十五枪(窃听风云之电话录音上传)

前一段时间我写过一篇关于短信监听的文章Android实战简易教程-第四十枪(窃听风云之短信监听),话说现在短信用的越来越少了啊,下面来个更猛的,电话录音监听上传,电话接通后开始录音,电话挂断后将录音上传.这里我们还是借助Bmob提供的上传服务,将录音文件上传到bomb的服务器,可以自行下载,播放录音. 一.配置bmob 配置bmob服务很是简单,注册账号,下载jar包,将jar包引入libs文件目录下: 然后配置权限: <uses-permission android:name="andr

2018年最新手把手教你搭建中小型互联网公司后台服务架构与运维架构

本课程主要是针对如何从无到有搭建中小型互联网公司后台服务架构和运维架构的课程,课程所涉及的内容均是当前应用最广泛的技术和工具.本课程所讲解的技术体系已经在多个中小型互联网公司中实战运行使用,目前运行已经非常稳定,数据量也是在不断持续增加.并且,这个技术体系也正在被其他很多互联网公司应用,希望通过此课程,让大家能快速熟练掌握各个技术,并且能实际应用到项目中.课程将会通过实际案例讲解,并且会提供完整的视频案例源码供学员学习使用,同时有需要的企业或学员可以直接拿本套教学案例代码来使用或者二次开发. 本

电话录音怎么在线转换成文字内容

电话录音怎么在线转换成文字内容,很多朋友在办公中都会遇到接听电话,有的时候电话内容过于重要需要将其录音下来保存成文字内容递交给上级,那这是怎么进行操作的呢,小编这就来告诉你.第一步:将需要转换成文字的语音在电脑上准备好,然后再通过电脑浏览器搜索迅捷语音云服务,来帮助我们进行转换.第二步:进入到语音与服务页面后,在页面中心可以看到两个转换选项,根据需要从中选择语音转文字的选项即可.第三步:进入到语音转换文字的选项后,就可以通过页面中心的录音图标将语音文件添加进来了.第四步:语音文件添加进来后,在文

问题解决系列: 后台服务流量控制- 控制访问别的服务的速度

互联网的后台提倡大系统小做,微服务化.所以后台服务之间相互依赖,我依赖别人的,别人也依赖我的,这很正常. 但是后台服务讲稳定性.只有一切可控,才能谈稳定性. 为了不冲垮下游的服务,我们有两种做法:一种是下游服务做一个自我保护(具体实现方法下次再写),一种是上游保护下游. 比如A服务向B服务发送消息,B给A分配了每分钟3000条消息的访问量.那么A如何控制自己每分钟的访问量在3000次以内呢? 基本思路: 这是个分布式的问题,A服务可能包含了堕胎机器,所有的机器共享一个设定的配额 3000次/每分

highchart访问一次后台服务返回多张图表数据

本文承接上一篇,我们制作动态图表的时候,往往需要的不止一张图表,如果每张图表都与服务接口做一次交互的话未免太过频繁,这无论对前后还是后台都是一种压力,本文介绍一种一次访问返回多组数据的方式来减少前台与后台服务的交互,闲话少说,查看动态效果  →.→ 详细代码 ←.← 如上文所示,highchart 的 chart 属性可以添加 events 事件,上文中我们插入的事件为: events: { load: function () { var series = this.series[0]; var