让后台服务不被杀———电话录音
来源:互联网 发布:图标上下不停跳动js 编辑:程序博客网 时间:2024/06/13 21:24
最近做了一个监听电话并录音的小例子,而保证它在后台运行不被杀确实不容易。
首先在主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>
0 0
- 让后台服务不被杀———电话录音
- android如何让后台服务service不被杀死(设置前台服务)
- Android开发之如何监听让服务不被杀死
- Android服务不被杀
- 让ASP木马不被杀
- 怎么让 Android 程序一直后台运行,像 QQ 一样不被杀死?
- android service后台服务进程的自启动和保持不被杀了
- android——关于不被杀掉的service
- Android开发之如何监听让服务不被杀死(service+broadcast)
- Android开发之如何监听让服务不被杀死(service+broadcast)
- Android开发之如何监听让服务不被杀死(service+broadcast)
- Android开发之如何监听让服务不被杀死(service+broadcast)
- Android App 防止 后台服务 被杀掉
- android如何让service不被杀死
- android如何让service不被杀死
- android如何让service不被杀死
- android如何让service不被杀死
- android如何让service不被杀死
- iOS框架介绍——N&O&P字头
- Dead Reckoning - 航位推测法
- 类的再生
- Lucene学习(一):全文检索的基本原理
- 将iframe设置为可编辑状态
- 让后台服务不被杀———电话录音
- 【详解】Python脚本转可执行文件
- 一个切换button背景的方法:getBackground(),setBackgroundDrawable
- UART0串口编程系列 串口(UART0)之UC/OS(一)
- 百度地图API 自定义标注图标
- Fragment切换时不会重复创建使数据多次加载即重复实例化
- Android 实现MD5加密
- MoPub sdk集成
- PHP中设置时区方法小结