无法启动服务意图

发布于 2024-11-08 11:26:27 字数 1539 浏览 0 评论 0原文

我已经阅读了大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

固执像三岁 2024-11-15 11:26:27

沟通班在哪里?

在清单中,您使用 android:name=".Communication" 声明服务,这意味着您的服务类应位于 com.exercise.AndroidClient.Communication

检查包裹是正确的。注意“.” (点)指的是包的根(即清单中声明的​​包)。因此,例如,如果您的包是 com.exercise.AndroidClient 并且您的服务类位于 com.exercise.AndroidClient.services.Communication 下,您需要像这样声明服务this:

<service android:enabled="true" android:name=".services.Communication" />

或者指定完整的包:

<service android:enabled="true" android:name="com.exercise.AndroidClient.services.Communication" />

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 in com.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 under com.exercise.AndroidClient.services.Communication you need to declare the service like this:

<service android:enabled="true" android:name=".services.Communication" />

Or specify the full package:

<service android:enabled="true" android:name="com.exercise.AndroidClient.services.Communication" />
机场等船 2024-11-15 11:26:27

我遇到了同样的错误,原因是 AndroidManifest.xml 中未定义该服务。

I had the same error and the cause was, that the service was not defined in the AndroidManifest.xml.

满地尘埃落定 2024-11-15 11:26:27

另外,如果您的服务恰好存在于您所引用的库项目中,请检查您的project.properties 文件。

你必须添加这一行:

manifestmerger.enabled=true

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:

manifestmerger.enabled=true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文