更新:
在添加了建议的方法(doBindService()和doUnbindService())以及@Nick Campion建议的来自here的调用之后
我已经尝试了一段时间来运行这个服务,但似乎什么都不起作用-我知道我可能遗漏了一个分号或其他什么:)
程序调用startNotificationService(),然后日志显示日志消息...并且该应用程序继续运行,而不显示Service。我找不到高级任务杀手服务。救命!
XML (在清单中):
<service
android:icon="@drawable/icon"
android:label="Smart Spdate Service"
android:name="notifyService">
<intent-filter
android:label="FULL_PATH_NAME_HERE.updateService">
</intent-filter>
</service>服务呼叫
Log.v("NOTICE", "Notification Service was not found running - starting");
//startService(new Intent(this, notifyService.class));
startService(new Intent(notifyService.class.getName()));
//startService(new Intent(TweetCollectorService.class.getName()));
/* FROM GOOGLE */
void doBindService() {
// Establish a connection with the service. We use an explicit
// class name because we want a specific service implementation that
// we know will be running in our own process (and thus won't be
// supporting component replacement by other applications).
this.bindService(new Intent(this, updateService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
/* END OF GOOGLE CODE */
@Override
public void onDestroy() {
web.close();
doUnbindService(); // Added to `onDestroy` - suggested by Google page
super.onDestroy();
Log.v("NOTICE", "PROGRAM TERMINATED");
}updateService.java
public class updateService extends Service {
private String TAG = "SERVICE";
public static final int INTERVAL = 60000;
private Timer timer = new Timer();
private static updateService Pointer;
public updateService() {
Pointer = updateService.this;
}
public static class LocalBinder extends Binder {
static updateService getService() {
return Pointer;
}
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
if (timer != null) {
timer.cancel();
}
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
doStuff();
}
}, 0, INTERVAL);
super.onStart(intent, startId);
}
public void doStuff() {
Log.v(TAG, "doStuff");
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();}
发布于 2011-03-02 12:55:06
我看不到您的客户端绑定到您的服务的任何地方。看一看local service example.。即使调用startService也要使用绑定模式的原因是因为startService调用是异步的。您需要进行额外的调用来绑定服务,以确保在启动完成后收到回调。
我发现在NPR Open Source App中有一个非常好的client和service服务示例,可供您学习!
https://stackoverflow.com/questions/5163487
复制相似问题