Android服务onStartCommand从未调用
我注意到,Android服务onStartCommand从未调用,android,android-service,Android,Android Service,我注意到,Service.START\u STICKY不起作用,当我仔细查看时,我看到onCreate()正在运行,但没有调用onstart命令 你知道为什么吗 @Override public void onCreate() { mGlobalData = GlobalData.getInstance(); mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVIC
Service.START\u STICKY
不起作用,当我仔细查看时,我看到onCreate()正在运行,但没有调用onstart命令
你知道为什么吗
@Override
public void onCreate() {
mGlobalData = GlobalData.getInstance();
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (mTimer == null)
mTimer = new Timer();
Log.e(TAG, "onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
int t = START_STICKY;
Log.e(TAG, "call me redundant BABY! onStartCommand service");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return t;
}
尝试插入行
android.os.Debug.waitForDebugger()代码>在onCreate()的末尾。在我这样做之前,调试器没有达到我的onStartCommand()
断点。如果您遇到与我相同的情况,我的服务
启动并运行良好(onCreate()
和onServiceConnected()
都被调用),但从未调用过onStartCommand(Intent,int)
。我发现这是因为系统启动了我的服务
,而不是我在代码中明确启动了服务
。根据报告:
每当客户端通过调用startService(Intent)
显式启动服务时,系统就会调用[onStartCommand(Intent,int)
因此,我必须在代码中显式调用startService(newintent(context,MyService.class))
以获取onStartCommand(Intent,int)
来触发。请注意,这样做不会重新启动系统创建的服务,也不会创建该服务的新实例。对我有效。但是当我从onCreate方法中删除它以确认这实际上就是问题时,问题就消失了。