让后台服务不被杀———电话录音
最近做了一個監聽電話并錄音的小例子,而保證它在后臺運行不被殺確實不容易。
首先在主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 ??
另外建再建一個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
總結
以上是生活随笔為你收集整理的让后台服务不被杀———电话录音的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVC3.0删除数据的时候给提示信息
- 下一篇: [Swift算法]巴比伦法(牛顿迭代法)