更新:
添加建议的方法(doBindService()
和 doUnbindService()
)以及调用后无济于事)来自 here由 @Nick Campion 建议
我已经尝试了一段时间来运行此服务,但似乎没有任何效果 - 我知道我可能缺少分号或其他东西:)
程序调用startNotificationService()
,然后日志显示日志消息...并且应用程序继续运行,但服务不显示。我找不到提前服务任务 killer 。救命!!!
XML( list 中):
<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();
}
最佳答案
我没有看到您的客户端与您的服务绑定(bind)的任何地方。看看local service example. 。即使调用 startService 也使用绑定(bind)模式的原因是因为 startService 调用是异步的。您需要进行额外的调用来绑定(bind)服务,以确保启动完成后您能得到回调。
我发现了一个非常好的服务示例 client和 service可以在 NPR Open Source App 中找到供您学习!
关于java - Android 服务无法启动 - 在 list 中...但什么也没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163487/