我已经阅读了大约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>
非常感谢您的建议。
startService(new Intent(getApplicationContext(), Communication.class));
更改为startService(new Intent(getApplicationContext(), com.client.Communication.class));
并在清单文件中进行相同的更改,解决了此问题。我以为由于所有文件都在同一个包中,所以这样做没问题...看来不是这样。 - Shaun