I've been struggling with this for 2 weeks now.
我已经挣扎了两个星期了。
I'm developing an app that controls call duration.
我正在开发一个控制通话时长的应用程序。
I receive a braodcast where I start a foreground service to hang up the call.
我收到一个braodcast,在那里我启动前台服务来挂断电话。
but after five minutes or more android force stop my package then kills my process which cause the service to crash -I think - (don't know why) with no crash message or any kind of error.
但在五分钟或更长时间后,android force停止了我的包,然后终止了导致服务崩溃的进程——我想——(不知道为什么)没有崩溃消息或任何类型的错误。
it just disappear.
它只是消失。
And it Schedules restart but it never start the service again.
它安排重新启动,但再也不启动服务。
here is the logcat
这是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}
using adb shell dumpsys
I got this which insures that my service is a foreground service with the right priority
使用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}
and in Process LRU list (sorted by oom_adj):
section
在进程LRU列表中(按oom_adj排序):section
Proc #24: adj=prcp /FS trm= 0 27922:club.androidy.callcontrolfree/u0a10088 (fg-service)
I'm not binding my service to any activities
我不会把我的服务和任何活动捆绑在一起
I start my service like this:
我的服务是这样开始的:
Intent srvIntent = new Intent(context, PhoneCallService.class);
context.stopService(srvIntent);
PhoneCallService.stopService = false;
context.startService(srvIntent);
and this is my onStartCommand
:
这是我的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;
}
I'm acquiring wake lock like this
我得到了像这样的唤醒锁
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");
}
I have already tried many solutions from SO and google issues but nothing works for me.
我已经从SO和谷歌问题中尝试过很多解决方案,但是没有一个适合我。
I saw and tried all these :
我看到并尝试了所有这些:
Foreground service killed by OS
前台服务被OS杀死
Foreground service gets killed every time
前台服务每次都会被删除
Android foreground service being killed under certain conditions
Android前台服务在一定条件下被关闭
Foreground Service being killed on Notification click
前台服务在通知点击时被杀死。
Foreground service being killed by Android
前台服务被Android删除
Users who keep the app are only 30% from total installs.
拥有这款应用的用户只比安装总数少30%。
What I'm doing wrong?
我做错了什么吗?
3
Edit:
编辑:
If you have the same issue please check Power Saving Options in your phone settings if your phone has this feature it will kill your app after the screen is off and will not allow it to work in background.
如果你有同样的问题请在你的手机设置中检查省电选项如果你的手机有这个功能它会在屏幕关闭后关闭你的应用程序,并且不允许它在后台工作。
Original answer: After a lot of search I solved it myself
原始答案:经过大量的搜索,我自己解决了这个问题
it turned out that my app was power-intensive as shown in the picture below
结果显示,我的应用程序是功率密集型的,如下图所示。
Hence, android was force stopping it after 5 minutes when there is no activity on the screen to save power.
因此,android在5分钟后就停止了,因为屏幕上没有活动来节省电力。
I solved power consumption issues and everything became OK.
我解决了能耗问题,一切都好了。