- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
注意:这个问题假设您知道将服务绑定(bind)到上下文。
制作中的每个人都知道这个问题,甚至一些拥有 Android Oreo 或 Pie 设备的用户都知道。异常如下所示:
Fatal Exception: 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(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Context.startForegroundService(Intent)
您还必须调用
Service.startForeground(int, Notification)
5秒内 (即使您的手机甚至可能无法启动您的服务,但由于某种原因它成为您的错)或者您的应用程序将卡住或从 Android 框架抛出异常。经过进一步思考,我制定了一个解决方案。
Context.startForegroundService(Intent)
不保证该服务将在 5 秒内创建并且是一个异步操作,我认为这样会发生一些事情:
// Starts service.
public static void startService(Context context)
{
// If the context is null, bail.
if (context == null)
return;
// This is the same as checking Build.VERSION.SDK_INT >= Build.VERSION_CODES_O
if (Utilities.ATLEAST_OREO)
{
Log.w(TAG, "startForegroundService");
// Create the service connection.
ServiceConnection connection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
// The binder of the service that
// returns the instance that is created.
MyService.LocalBinder binder = (MyService.LocalBinder) service;
// The getter method to acquire the service.
MyService myService = binder.getService();
// getServiceIntent(context) returns the relative service intent.
context.startForegroundService(getServiceIntent(context));
// This is the key: Without waiting Android Framework
// to call this method inside Service.onCreate(),
// immediately call here to post the notification.
// MyService.createNotification(context) is static
// for other purposes.
myService.startForeground(19982, MyService.createNotification(context));
// Release the connection to prevent leaks.
context.unbindService(this);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
}
};
// Try to bind the service.
try
{
context.bindService(getServiceIntent(context), connection, Context.BIND_AUTO_CREATE);
}
catch (RuntimeException ignored)
{
// This is probably a broadcast receiver context
// even though we are calling getApplicationContext().
// Just call startForegroundService instead
// since we cannot bind a service to a
// broadcast receiver context.
// The service also have to call startForeground in this case.
context.startForegroundService(getServiceIntent(context));
}
}
else
{
// Normal stuff below API 26.
Log.w(TAG, "startService");
context.startService(getServiceIntent(context));
}
}
最佳答案
根据codelab ,它看起来像是在 Android 9.0+ 上实现前台服务的正确方法
关于android - Context.startForegroundService 然后没有调用 Service.startForeground : Alternative solution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55906296/
startForeground() 需要创建 NotificationChannel 以便在 Oreo 设备的 Launcher 图标上显示徽章编号 1 如何以编程方式隐藏/禁用它? 因为 Galax
我正在启动一个前台服务,它使用 HandlerThread 来执行一些长时间运行的任务。 它工作正常,但是,通知没有显示我的标题或内容(它说“xxx is running”点击获取更多信息或停止应用程
我创建了一个发送电子邮件的服务 (EmailService) ...每次我需要用我的应用程序发送电子邮件时,它都会启动该服务并通过 Intent 传递电子邮件的 ID... 我正在使用 startfo
我创建了一个服务。我启动了这个服务前台,但是当我午餐我的应用程序时,一个应用程序崩溃了。我添加了 startServiceOreoCondition 我读到了这个但是它对我没有帮助 @Override
我正在尝试使用 while 循环来每秒更新一次通知。但是在 2.3.3 及以下版本中,它会因这些 logcat 错误而崩溃: 08-14 07:30:17.394: E/AndroidRuntime(
我正在尝试让我的 Service 在前台运行。我尝试使用 this example (请查看“在前台运行服务”部分),但 startForeground() 实际上并没有显示我的通知。并且不会抛出任何
我想创建一个服务并让它在前台运行。 大多数示例代码都有通知。但我不想显示任何通知。那可能吗? 你能给我一些例子吗?有没有其他选择? 我的应用服务正在播放媒体播放器。 如何让系统不杀死我的服务,除了应用
所以我不确定在哪里/如何实现此方法以使我的服务在前台运行。目前我在另一个 Activity 中通过以下方式开始我的服务: Intent i = new Intent(context, myServic
错误如下,我正在尝试启动此服务以提供持续的传感器更新,目标是注销传感器事件直到我停止服务,我只需要能够启动此服务来收集数据直到我停止服务: android.app.RemoteServiceExcep
背景:在 android 中使用 startForeground() 命令时,用户必须看到一条通知,说明您的应用程序在前台运行。 我的问题:我正在考虑使用以下 command,而不是在我的服务中调用
问题 我想知道我们是否需要获取 WakeLock,或者 service.startForeground() 命令是否为我们完成了?我没有在文档中找到任何内容。 目标 我们希望删除可能会减慢服务启动速度
我正在使用 startForeground()调用我的一项服务: NotificationCompat.Builder notifBuilder =
我遇到了一个问题,即使我持有唤醒锁并且调用了 startForeground,我的服务还是被终止了。当平板电脑 (ASUS Transformer TF101) 发生这种情况时,停止服务而不调用 on
是否可以防止 Service(startForground) 被 os 杀死?我用它来播放音频但是当我使用另一个需要更多资源的应用程序(愤怒的小鸟去)一段时间时它被杀死了。在日志中,我看到应用进程被
我有一个 IntentService,我想通过持续通知让它保持粘性。问题是通知出现然后立即消失。该服务继续运行。我应该如何在 IntentService 中使用 startForeground()?
我有一个包含两项服务的应用。 一个是使用 WindowManager 显示在其他应用程序上 float (覆盖)的 UI。另一个是使用 GooglePlayAPI 进行位置跟踪。我的应用始终运行这些服
我的 IntentService 有问题。每次我启动服务时,一旦服务空闲,就会调用 onDestroy() 方法。我将我的服务设置为在前台运行,尽管如此,该服务仍会立即被终止。我的应用程序中只有一项其
即使有前台服务,我的 Android 应用程序也会在后台被杀死。这是漏洞 list :
作为测试,我从我们拥有的工作服务中删除了“startForeground(通知)”,它似乎仍在工作。 我们是否不需要在 Android O 及更高版本上发布工作服务的前台通知? 我无法找到这方面的具体
我正在使用 Xamarin Android 3.5 开发一项服务。我们的应用程序面向 Android 8.1 (API 27 - Oreo)。我希望该服务作为前台服务运行。但是,当我运行该服务时,出现
我是一名优秀的程序员,十分优秀!