无法启动服务意图
我已经阅读了大约 100 个关于这个问题的问题和答案,但我似乎无法让它发挥作用。我正在尝试从 Activity
启动 Service
。我的清单文件似乎没问题,我启动 Service
的方式似乎也是正确的。 LogCat 中显示以下错误:
ActivityManager(1296): Unable to start service Intent
{ cmp=com.exercise.AndroidClient/com.client.Communication }: not found
我试图通过在我的 Activity
中调用此服务来启动服务:
startService(new Intent(getApplicationContext(), Communication.class));
Service
如下:
public class Communication extends Service {
public Communication() {
super();
}
@Override
public void onCreate() {
super.onCreate();
Log.i("Service", "Created service");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Service", "onStartCommand called");
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
我的清单文件中的条目是:
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidClient" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/sms" android:label="@string/app_name" >
<activity> ... </activity>
<service android:enabled="true" android:name=".Communication" />
</application>
</manifest>
非常感谢任何建议。
I've read probably 100 questions and answers on this issue, but I cant seem to get this working. I'm trying to start a Service
from an Activity
. My manifest file seems OK, the way I'm starting the Service
also seems to be correct. The following error is showing up in LogCat:
ActivityManager(1296): Unable to start service Intent
{ cmp=com.exercise.AndroidClient/com.client.Communication }: not found
I'm attempting to start the service by calling this in my Activity
:
startService(new Intent(getApplicationContext(), Communication.class));
The Service
is the following:
public class Communication extends Service {
public Communication() {
super();
}
@Override
public void onCreate() {
super.onCreate();
Log.i("Service", "Created service");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Service", "onStartCommand called");
return START_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
The entry in my manifest file is:
<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidClient" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/sms" android:label="@string/app_name" >
<activity> ... </activity>
<service android:enabled="true" android:name=".Communication" />
</application>
</manifest>
Any advice is greatly appriciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
沟通班在哪里?
在清单中,您使用
android:name=".Communication"
声明服务,这意味着您的服务类应位于com.exercise.AndroidClient.Communication
检查包裹是正确的。注意“.” (点)指的是包的根(即清单中声明的包)。因此,例如,如果您的包是
com.exercise.AndroidClient
并且您的服务类位于com.exercise.AndroidClient.services.Communication
下,您需要像这样声明服务this:或者指定完整的包:
Where is the Communication class?
In your manifest you declare a service with
android:name=".Communication"
, this means that your service class should be located incom.exercise.AndroidClient.Communication
Check that the packages are correct. Note that the "." (dot) refers to the root of your package (ie the package declared in the manifest). So, for example, if your package is
com.exercise.AndroidClient
and your service class is undercom.exercise.AndroidClient.services.Communication
you need to declare the service like this:Or specify the full package:
我遇到了同样的错误,原因是 AndroidManifest.xml 中未定义该服务。
I had the same error and the cause was, that the service was not defined in the AndroidManifest.xml.
另外,如果您的服务恰好存在于您所引用的库项目中,请检查您的project.properties 文件。
你必须添加这一行:
Also, if your service happens to exist in a library project that you are refering to, check your project.properties file.
You have to add this line: