将手机升级到8.1开发人员预览后,我的后台服务不再正常启动.
在我长期运行的服务中,我实现了一个startForeground方法来启动正在进行的通知,该通知在create上调用.
@TargetApi(Build.VERSION_CODES.O)
private fun startForeground() {
// Safe call, handled by compat lib.
val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)
val notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.build()
startForeground(101, notification)
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main
Process: $PACKAGE_NAME, PID: 24704
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Run Code Online (Sandbox Code Playgroud)
服务通知的通道无效,显然我的旧通道DEFAULT_CHANNEL_ID不再适用于我假设的API 27.什么是合适的渠道?我试图浏览一下文档
service android background-service android-notifications kotlin
我想让我Service在前台跑步.我尝试使用此示例(请查看" 在前台运行服务 "部分),但startForeground()实际上并未显示我的通知.并没有抛出异常.为了使它显示,我需要使用NotificationManager像这里解释的那样.随着NotificationManager我的通知工作,但我不确定我Service的前景是在这个"无声"呼叫之后startForeground().
有什么不对?
编辑:我刚刚测试了这个应该演示的示例项目startForeground(),但它不起作用!我使用API v7.0,我在模拟器和真实设备(SE Xperia Neo)上测试了它.通知不会出现.
EDIT2:如果我试着打电话,setForeground()那我就收到了警告setForeground: ignoring old API call.
我也尝试使用这里startForegroundCompat()描述的,但效果绝对相同.我按照这里的描述检查我的服务是否是前台服务,我发现我的服务不是前台服务.ActivityManager.RunningServiceInfo
首先,我看了看这些;
我有将近一百万人使用的流应用程序。我正在为播放器使用前台服务。我还没有实现MediaSession。我有99.95%的无崩溃会话。因此,该应用程序适用于所有版本,但我开始使用Android 9获取崩溃报告(ANR)。此崩溃仅发生在Samsung手机上,尤其是s9, s9+, s10, s10+, note9型号。
我尝试过这些
startForeground()方法onCreate()Service.startForeground()之前致电Context.stopService()我阅读了Google开发人员的一些评论,他们说这仅仅是Intended Behavior。我想知道这是由三星的系统还是Android操作系统引起的。有人对此有意见吗?我怎样才能解决这个问题?
我在文档中读到Context.startForegroundService()了隐含的承诺,即已启动的服务将调用startForeground().但是,由于Android O即将推出后台和前台服务的更改,与使用旧startService()方法相比,是否还有其他性能改进,或者它只是最佳实践?
我遇到了 Playstore 报告的大量异常。来自 Android P 的 RemoteServiceException。
我正在创建 android 前台服务,然后调用 startForegound(with channel)。
但 99.9% 只有 9.0(android P) 用户报告 RemoteServiceException。我检查了我是否为该服务制作了通知渠道。我还检查了我是否在 OREO 之后为 os 调用了 startForegroundService。
每个代码都没有问题。
但是我发现我多次调用 startForegroundService() ,但是在第一次创建 Service 时只调用了一次 onCreate() 。所以 onCreate() 中的 startForeground() 只被调用一次。
但是,如果我将 startForeground() 放在 onStartCommand() 中,那么它的调用次数也将与我调用 startForegroundService() 的次数一样多。因为每当您调用 startService/startForegroundService 时它也会被调用(即使已经创建了 Service 的实例)。
你认为这是异常的原因吗?
而 mboy 对/sf/answers/3587621901/的评论 也说了类似的话。
android foreground-service remoteserviceexception android-9.0-pie
我目前正在使用XMPP构建一个聊天应用程序.我已经创建了一个服务来处理连接和传入消息,添加了所需的不同侦听器.
然而,问题是,只要活动调用解除对服务的绑定(例如,当用户将应用程序置于后台时活动暂停或停止),服务就会被破坏,即使它内部有监听器(例如聊天)监听器,消息监听器等.)
如何在应用程序处于后台时能够接收消息,我如何保持服务的活动状态?我读到使用前台服务是非常不满意的,所以如果可能的话我宁愿避免使用它.
我的用户报告了数百次崩溃,但仍然无法找到解决方法.这些崩溃来自Android 8(三星,华为,谷歌).
我得到这两个崩溃:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Run Code Online (Sandbox Code Playgroud)
另一个:
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2104)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7428)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Run Code Online (Sandbox Code Playgroud)
我假设这些崩溃是相同的,但正如您所看到的,堆栈跟踪显示不同的代码行.
问题是我无法重现它,我的设备和模拟器上的一切正常.但是,我(不知何故)通过创建服务而不调用类startForeground()内来复制Service.
我无法"捕获"异常,因为它在创建服务5秒后立即来自系统级别.
我做了什么,我创建了一个方法,它创建一个粘性通知并调用startForeground方法(我的服务类):
private void startWithNotification() {
Resources res = getResources();
String title = res.getString(R.string.application_name);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
NotificationCompat.Builder …Run Code Online (Sandbox Code Playgroud) 这是我的BroadcastReciever类。该类正在处理启动电话状态。
代码;
public class BroadCastRecieverBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent ?ntent) {
if(Intent.ACTION_BOOT_COMPLETED.equals(?ntent.getAction()))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MyService.class));
context.startForegroundService(new Intent(context, GPSTracker.class));
} else {
context.startService(new Intent(context, MyService.class));
context.startService(new Intent(context, GPSTracker.class));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误;
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1792)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Run Code Online (Sandbox Code Playgroud)
现在,它不适用于Android Oreo。我不知道那是什么错误。
我有很多异常的逮捕,但是我不知道怎么可能,因为不了解问题的根源。这是异常逮捕的日志:
android.app.RemoteServiceException:
android.os.Handler.dispatchMessage(Handler.java:105)上的
android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1881)android.os.Looper.loop(Looper.java:164 ) android上的
android.app.ActivityThread.main(ActivityThread.java:6938)
java.lang.reflect.Method.invoke(本机方法)
com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327) .android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
其他信息是该问题在配备Android:7或更高版本的设备上更常见。
提前致谢
java android android-activity google-console-developer android-vitals
我的应用程序有一个服务,它JobIntentService从AppCompat 扩展类以处理设备上的一些数据并将其发布到HTTPs地址.
代码非常简单,就像互联网/文档中提供的任何样本一样:
我怎么称呼JobIntentService?
(我叫这个FirebaseMessagingService)
val intent = Intent()
intent.putExtra(SOME_INFO, data["someInfo"]?.toInt() ?: INVALID_VALUE_INT)
MyCoolService.enqueueWork(applicationContext, intent)
Run Code Online (Sandbox Code Playgroud)
JobIntentService看起来怎么样?
class MyCoolService: JobIntentService() {
companion object {
val JOB_ID = 1001
fun enqueueWork(context: Context, work: Intent) {
enqueueWork(context, MyCoolService::class.java, JOB_ID, work)
}
}
override fun onHandleWork(intent: Intent) {
//Heavy work here... or empty, doesn't matter... The error happens too.
}
}
Run Code Online (Sandbox Code Playgroud)
表现:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:fullBackupContent="true"
...>
<service android:name="com.my.app.MyCoolService"
android:label="some info service"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>
<application>
Run Code Online (Sandbox Code Playgroud)
logcat的: …
由于Android Oreo后台执行限制,因此文档建议将IntentServices 重构为JobIntentService。
https://developer.android.com/about/versions/oreo/background
JobIntentService立即以IntentService低于Oreo的价格运行,但在Oreo +上安排作业
https://developer.android.com/reference/android/support/v4/app/JobIntentService
在什么情况下,将法线IntentService作为Service具有持久性通知的前台会有意义,什么时候JobIntentService更好?
我可以看到的一个缺点JobIntentService是它不会立即开始。
android android-service intentservice android-intentservice android-jobscheduler
在检查了谷歌问题和很多其他问题之后,我找到了我在底部添加的解决方案。
我所关心的事情如下:
onCreateAND 中onStartCommand,我确保将服务移至前台(如果它尚未位于前台)onStartCommand处理程序的额外命令来执行此操作,以确保服务在启动时不会停止(最终在它完成进入前台之前)context.stopService(...)),我总是通过正在运行的服务本身的命令来停止服务 - 所以我确保它只能在它在前台运行时停止,而不是在启动期间停止我个人使用该服务进行覆盖,因此我不在类中处理活页夹,因为我不使用它们。
我得到的日志
2019-07-29 21:41:27,146 [[BaseOverlayService:62 onCreate]]: onCreate
2019-07-29 21:41:27,146 [[BaseOverlayService:142 b]]: BEFORE moveToForeground (called by onCreate)
2019-07-29 21:41:27,152 [[BaseOverlayService:159 b]]: AFTER moveToForeground (called by onCreate) - moved to foreground: true
2019-07-29 21:41:27,176 [[BaseOverlayService:79 onStartCommand]]: onStartCommand: isForeground: true | action: null | isStopping: false
2019-07-29 21:41:27,945 [[BaseOverlayService:142 b]]: BEFORE moveToForeground (called by updateNotification [OverlayService [onInitFinished]])
2019-07-29 21:41:27,947 [[BaseOverlayService:159 b]]: AFTER moveToForeground (called …Run Code Online (Sandbox Code Playgroud) 我从以下异常输出Device Monitor:
//FATAL EXCEPTION: main
//Process: com.xxx.yyy, PID: 11584
//android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
// at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
// at android.os.Handler.dispatchMessage(Handler.java:105)
// at android.os.Looper.loop(Looper.java:164)
// at android.app.ActivityThread.main(ActivityThread.java:6944)
// at java.lang.reflect.Method.invoke(Native Method)
// at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
// at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Run Code Online (Sandbox Code Playgroud)
我的应用程序中有 4 个服务,它们基本上都是这样的:
public class MyService extends Service {
private static final int forgroundId = 55544433; //Unique for each Service
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) { …Run Code Online (Sandbox Code Playgroud) service android android-notifications foregroundnotification