无法启动服务Intent

Sha*_*aun 6 service android

我在这个问题上已经阅读了大约100个问题和答案,但我似乎无法让这个工作.我正试图Service从一个开始Activity.我的清单文件似乎没问题,我启动的方式Service似乎也是正确的.LogCat中显示以下错误:

ActivityManager(1296): Unable to start service Intent
{ cmp=com.exercise.AndroidClient/com.client.Communication }: not found
Run Code Online (Sandbox Code Playgroud)

我试图通过在我的调用中启动该服务Activity:

startService(new Intent(getApplicationContext(), Communication.class));
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的清单文件中的条目是:

<?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>
Run Code Online (Sandbox Code Playgroud)

任何建议都非常适合.

aro*_*ero 4

沟通班在哪里?

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

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

<service android:enabled="true" android:name=".services.Communication" />
Run Code Online (Sandbox Code Playgroud)

或者指定完整的包:

<service android:enabled="true" android:name="com.exercise.AndroidClient.services.Communication" />
Run Code Online (Sandbox Code Playgroud)