我创建了一个名为HelloService的新类.我将其添加到Android manifest.xml中.
public class HelloService extends Service {
private Timer timer = new Timer();
private long INTERVAL = 5000;
public void onCreate() {
super.onCreate();
startservice();
}
private void startservice() {
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {
Log.d("servy", "This proves that my service works.");
}
}, 0, INTERVAL);
; }
private void stopservice() {
if (timer != null){
timer.cancel();
}
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我的其他活动称之为:
Intent helloservice = new Intent(this, HelloService.class);
startService(helloservice); …Run Code Online (Sandbox Code Playgroud) 我在这个问题上已经阅读了大约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 …Run Code Online (Sandbox Code Playgroud)