![](/img/trans.png)
[英]Foreground service not stopping after killing and relaunching app - Android Studio
[英]Android is killing my foreground service after Force stop my package
我已經在這個問題上苦苦掙扎了兩個星期了。
我正在開發一個控制通話時長的應用程序。
我收到一個braodcast,我開始一個前台服務來掛斷電話。
但是在五分鍾或更長時間之后,android強制停止我的程序包然后殺死我的進程導致服務崩潰 - 我認為 - (不知道為什么)沒有崩潰消息或任何類型的錯誤。
它就消失了。
它安排重啟但它永遠不會再啟動服務。
這是logcat
12-29 00:28:52.857 619-619/? I/ActivityManager: Force stopping package club.androidy.callcontrolfree appid=10006 user=0
12-29 00:28:52.858 619-619/? I/ActivityManager: Killing proc 433:club.androidy.callcontrolfree/u0a10006: force stop club.androidy.callcontrolfree
12-29 00:28:52.894 619-619/? W/ActivityManager: Scheduling restart of crashed service club.androidy.callcontrolfree/.PhoneCallService in 5000ms
12-29 00:28:52.919 619-619/? I/ActivityManager: Force stopping service ServiceRecord{422b1b18 u0 club.androidy.callcontrolfree/.PhoneCallService}
使用adb shell dumpsys
我得到了這個,以確保我的服務是具有正確優先級的前台服務
*APP* UID 10088 ProcessRecord{41aefb98 27922:club.androidy.callcontrolfree/u0a10088}
user #0 uid=10088
class=club.androidy.callcontrolfree.AnalyticsApplication
dir=/data/app/club.androidy.callcontrolfree-1.apk publicDir=/data/app/club.androidy.callcontrolfree-1.apk data=/data/data/club.androidy.callcontrolfree
packageList=[club.androidy.callcontrolfree]
compat={240dpi}
thread=android.app.ApplicationThreadProxy@41cef800
pid=27922 starting=false lastPss=0
lastActivityTime=-11s768ms lruWeight=14097791 serviceb=false keeping=true hidden=false empty=true
oom: max=15 hidden=9 client=9 empty=15 curRaw=2 setRaw=2 nonStopping=2 cur=2 set=2
curSchedGroup=-1 setSchedGroup=-1 systemNoUi=false trimMemoryLevel=0
adjSeq=63108 lruSeq=10968
setIsForeground=false foregroundServices=true forcingToForeground=null
lastRequestedGc=-12s74ms lastLowMemory=-12s74ms reportLowMemory=false
Services:
- ServiceRecord{41fb2578 u0 club.androidy.callcontrolfree/.PhoneCallService}
並在Process LRU list (sorted by oom_adj):
section
Proc #24: adj=prcp /FS trm= 0 27922:club.androidy.callcontrolfree/u0a10088 (fg-service)
我沒有約束我的服務任何活動
我開始這樣的服務:
Intent srvIntent = new Intent(context, PhoneCallService.class);
context.stopService(srvIntent);
PhoneCallService.stopService = false;
context.startService(srvIntent);
這是我的onStartCommand
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Context context = getApplicationContext();
String txtNotificationTicker = context.getString(R.string.strNotificationTicker);
String txtNotificationTitle = context.getString(R.string.strNotificationContexTitle);
String txtNotificationText = context.getString(R.string.strNotificationContexText);
String ns = Context.NOTIFICATION_SERVICE;
Intent cancelIntent = new Intent(this, StopServiceReceiver.class);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, cancelIntent
, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action =
new NotificationCompat.Action
.Builder(R.drawable.ic_cancel
, context.getString(R.string.stop_service), pendingIntentCancel)
.build();
Intent mIntent = new Intent(this,MainActivity.class);
int HELLO_ID = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, HELLO_ID, mIntent , 0);
this.mNotificationManager = (NotificationManager) getSystemService(ns);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder
.setContentIntent(pendingIntent)
.setContentTitle(new StringBuilder(String.valueOf(txtNotificationTitle)))
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(getBitmap(context, R.drawable.ic_launcher))
.setContentText(new StringBuilder(String.valueOf(txtNotificationText))
.append(": ").append(PHONE_NUMBER).toString())
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_MAX)
.addAction(action)
.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(1, notification);
this.pt = new PhoneThread();
this.pt.start();
return Service.START_STICKY;
}
我正在收購這樣的喚醒鎖
if (PhoneCallService.sCpuWakeLock == null) {
PhoneCallService.sCpuWakeLock = ((PowerManager)
context.getSystemService(Context.POWER_SERVICE))
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"CCF");
PhoneCallService.sCpuWakeLock.acquire();
Log.e("androidy","wacklock acquired");
}
我已經嘗試過SO和谷歌問題的許多解決方案,但對我來說沒有任何作用。
我看到並嘗試了所有這些:
保留該應用的用戶僅占總安裝量的30%。
我做錯了什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.