- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想使用 bindService
方法创建服务。但是当我关闭一个 Activity 时,我的服务被破坏了,我不希望这样。
我尝试使用 startForeground(NOTIFICATION_ID, notification);
service onCreate 将服务置于前台,但服务仍然被破坏。
现在我尝试调用两种方法同时启动服务:
Intent bindIntent= new Intent(this, ServiceC.class);
startService(bindIntent);
bindService(bindIntent, onService, BIND_AUTO_CREATE);
通过调用这两个方法Service没有被销毁。我的应用程序使用这种方法运行良好。
有人可以向我解释这是否是一个好方法,如果不是,请告诉我为什么 startForeground(NOTIFICATION_ID, notification);
不起作用?
使用 bindService 的最佳方式是什么,但同时我不希望该服务自行销毁。
最佳答案
我使用了相同的解决方案,这是一个合法的解决方案。来自 Service引用:
A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the service's onDestroy() method is called and the service is effectively terminated.
startForeground()
不起作用,因为它只是试图防止服务被系统杀死,但它的生命周期是另一回事:如果没有什么更绑定(bind)到该服务并且它不是'它开始了,它就停止了。
关于Android bindService 或/和 startService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3689460/
Context.startService(intent) 和 startService(intent) 有什么区别,使用哪个有关系吗? 最佳答案 只有一个startService() 方法。 star
这只是一个知识问题,我知道这样做不是一个好的做法。所以这里是: 如果我调用 startService() 3 次而它已经在运行,我是否必须调用 stopService() 3 次才能让它停止? (它有
我有一个依赖 X 的 Windows 服务 S1。该服务是自动的,它可以毫无问题地启动。 此服务启动另一个具有相同依赖项 X 的服务 S2。问题是,当我调用 StartService() 启动 S2
我尝试从服务类启动 android 服务。这样做的原因是为了实现一些平台独立性。 这样做我在 android.content.ContextWrapper.startService(ContextWr
我有这个函数作为静态方法: public static void engine(SQLiteDatabase mydb){ Intent intent = new Intent
我在文档中的任何地方都找不到这个: 对 context.startService() 的调用是同步的还是异步的? 最佳答案 异步。它甚至不会开始做任何工作,直到您从您所在的任何回调(例如,onCrea
startService() 是创建一个新的服务实例还是使用现有的?例如,在下面的代码中,它是创建两个 UpdateService 实例还是只创建一个 UpdateService 实例?谢谢。
我正在尝试安装 Neo4j(1.8),但在启动服务时遇到问题。 解压zip文件后,我直接进入bin文件夹并执行批处理文件: Neo4J.bat install [SC] 创建服务成功 [SC] 启动
我正在 Delphi 10.2 pro 中编写一些服务应用程序,我想添加一个启动时间可控参数来强制服务应用程序进入启动等待循环足够长的时间,以便我可以单击进入“运行\附加到进程”窗口(在应用程序开始初
我有一个小脚本,用户可以在其中输入脚本名称(描述名称),并可以选择修改属性。这是通过GUI界面完成的:一个文本框和一些下拉列表。 我尝试了几种方法来实现它,但是没有一个起作用。 这是我如何检索服务信息
这件事让我非常头疼。 我正在尝试使用 CreateService 和 SERVICE_ALL_ACCESS 将驱动程序加载到进程中,在我实际尝试使用 StartService 启动服务之前,一切似乎都
这就是我启动位置服务的方式。 private void initLocationService() { locationManager = (LocationManager) getSyste
我已经创建了一个服务,我正在使用 bindService 函数通过我的 Activity 访问它,但我想在我的 Activity 退出后继续运行我的服务。 为此,我必须使用 startService,
我正在尝试在Windows 10(10.0.18362)上安装自签名内核驱动程序,这里是我遵循的步骤以及出现的错误。如何解决这个问题?谢谢 c:\>makecert -r -pe -ss Privat
我想从我的 Activity 类(class)中启动一项服务。问题是,onStartCommand 永远不会被调用。我在那里放置了断点,但它从未被击中。我的例子非常简单,我不明白为什么它不起作用。 M
我通过 Intent 在我的 Activity 和我的服务之间发送数据(我多次调用 startForegroundService 因为它再次调用 onStartCommand 并使用 getInten
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 我正在尝试使用 IntentSe
在 while(true) 语句中,我正在创建一个将在 onHandleIntent 中运行案例的 Intent 。但是,如果不将 Activity 类扩展到我的 LocationTracker 类,
我想用 C++ 程序启动卷影复制服务。这涉及获取服务控制管理器 (OpenSCManager) 的句柄,使用它来获取服务句柄 (OpenService),然后启动服务 (StartService)。
我继承了一个项目,但我不明白如何通过以下代码获得响应或设置回调: startService(new Intent(this, VenueUpdateService.class)); 目前,即使 Ven
我是一名优秀的程序员,十分优秀!