日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2025/3/21 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 让后台服务不被杀———电话录音 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近做了一個監聽電話并錄音的小例子,而保證它在后臺運行不被殺確實不容易。

首先在主service中:

service中重寫onStartCommand方法,這個方法有三個返回值,?START_STICKYservicekill掉后自動

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

總結

以上是生活随笔為你收集整理的让后台服务不被杀———电话录音的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。