jjzjj

StartService

全部标签

android - 在 bindService 之前等待 startService

据我了解,如果我希望服务在不受任何限制的情况下运行,则必须首先使用startService(Intenti)启动它。我的问题是,如果我想在启动后立即绑定(bind)到服务,下面的代码是否可以保证服务是使用startService()创建的?服务类中的静态方法:publicstaticvoidactStart(Contextctx){Intenti=newIntent(ctx,BGService.class);i.setAction(ACTION_START);ctx.startService(i);}绑定(bind)Activity:BGService.actionStart(getA

android - startService() 抛出 java.lang.IllegalStateException

从我的Activity的onCreate(),我正在尝试通过以下代码启动服务:Intentintent=newIntent();intent.setClassName(SERVICE_PKG_NAME,SERVICE_NAME);context.startService(intent);//context=MainActivity.this但是,我收到以下异常:java.lang.IllegalStateException:NotallowedtostartserviceIntent(service-name):appisinbackground知道这可能是什么原因吗?现在坚持了几个小

Android 服务 - 多次调用 startService 并导致值混淆......

我的代码有一些条件调用,这些调用使用通过bundle传递到该服务的不同数据值启动相同的服务。当我检查只满足一个条件时,服务在所有条件下都可以正常工作。但是当2个或更多条件匹配时,它们会调用相同的服务,但bundle中的数据值不同。问题是当满足这种情况时,第一个调用发送的值不会被替换为第二个条件以启动相同的服务。所以服务响应错误。是这样的if(somecond){somevaluesinserivceintentbundle.startService(serviceintent1);}if(somecond){somedatainintentbundle.startService(ser

android - 从 Service.onBind() 中调用 Service.startService() 可以吗?

我有一个服务,当不再有任何Activity绑定(bind)到它时,我想确保它不会停止()。我知道startService()用于实现此目的,但是可以从服务的onBind()方法中调用它吗?从Activity中调用它对我来说似乎是一个hack,因为服务最清楚它是否想在onUnbind()之后停留。Activity指示服务的当前状态并提供一些控制,因此bindService()似乎是适合在此处使用的方法。 最佳答案 从onBind()中调用startService()应该没问题。如果您在onBind()中有逻辑可以确定服务需要保持运行,

android - 多次调用 IntentService 的 startService 时 Intent 是否进入队列?

我想使用IntentService从Internet下载。我通过调用startService(intentserive);将url通过Intent传递给IntentService。如果我为各种Intent调用startService,这些Intent是否会排队等待下载? 最佳答案 您的问题的简短回答是肯定的。来自文档:IntentServiceisabaseclassforServicesthathandleasynchronousrequests(expressedasIntents)ondemand.Clientssendreq

Android:使用 Context.startService 与 PendingIntent.getService 启动服务

Context.startServiceIntentintent=newIntent(context,MyService.class);context.startService(intent);PendingIntent.getServiceIntentintent=newIntent(context,MyService.class);PendingIntentpi=PendingIntent.getService(context,0,intent,0);pi.send();问题您何时会使用Context.startService与PendingIntent启动服务?你为什么要用一个而

java - 对前台服务使用 Context.startForegroundService(Intent) 而不是 Context.startService(Intent) 有什么好处吗?

我读入了thedocsContext.startForegroundService()隐含promise启动的服务将调用startForeground()。然而,由于AndroidO正在对后台和前台服务进行更改,与使用旧的startService()方法相比,它是否有任何其他性能改进,或者它只是future的最佳实践? 最佳答案 它既不是性能改进,也不是yield,也不是最佳实践。从API26开始,系统不允许后台应用创建后台服务。因此,如果您的应用在后台(如果它也在前台也欢迎您这样做),您必须使用Context.startForeg

android - 这个警告是由我的应用程序引起的吗? - "Implicit intents with startService are not safe"

我在开发时在Logcat中收到此警告。是我的应用造成的吗?16699-16699/tld.me.myapp.debugW/ContextImpl﹕ImplicitintentswithstartServicearenotsafe:Intent{act=com.google.android.location.internal.GoogleLocationManagerService.START}android.content.ContextWrapper.bindService:517com.google.android.gms.internal.v.a:-1com.google.and

android - 这个警告是由我的应用程序引起的吗? - "Implicit intents with startService are not safe"

我在开发时在Logcat中收到此警告。是我的应用造成的吗?16699-16699/tld.me.myapp.debugW/ContextImpl﹕ImplicitintentswithstartServicearenotsafe:Intent{act=com.google.android.location.internal.GoogleLocationManagerService.START}android.content.ContextWrapper.bindService:517com.google.android.gms.internal.v.a:-1com.google.and

android - 防止 Android 服务的多个副本

doco对于startService状态“如果此服务尚未运行,它将被实例化并启动(如果需要为它创建一个进程);如果它正在运行,那么它仍然在运行。”我发现每次对startService的调用似乎都在启动服务的一个separate实例,因为该服务正在执行的工作(在我的测试用例中,简单地写入一个新的日志文件)每次通话都会再次完成。我试图通过循环来检测服务ActivityManager...getRunningServices(Integer.MAX_VALUE))但它没有出现。SGS11上的Android2.3.3我在这里遗漏了一些东西。我知道服务的onCreate()方法只有在创建时才会被