首页 > 试题广场 >

下列不属于 service 生命周期的方法是 ____

[单选题]
下列不属于 service 生命周期的方法是 ____
  • onCreate
  • onDestroy
  • onStop
  • onStartCommand
推荐
选择C
由于Android Service的生命周期并不像Activity那么复杂,它只继承了onCreate(),onStart(),onDestroy()三个方法,当我们第一次启动Service时,先后调用了onCreate(),onStart()这两个方法,当停止Service时,则执行onDestroy()方法,需要注意的是,如果Service已经启动了,当我们再次启动Service时,不会在执行onCreate()方法,而是直接执行onStart()方法。
编辑于 2017-03-18 10:08:02 回复(1)
哪有onStart这个方法,只有onStartCommand
发表于 2017-03-03 23:41:47 回复(5)
c, (1)startService方式: onCreate()->onStartCommand()->onDestroy() (2)bindService方式: onCreate()->onBind()->onUnBind()->onDestroy()
发表于 2017-05-12 10:49:39 回复(1)
我在官网及别人博客上看的
发表于 2017-03-02 16:57:47 回复(2)
什么老题,onStart方法是不知道多少个版本以前的名字了吧,现在改名为onStartCommand了
编辑于 2018-04-03 16:36:58 回复(0)
onStart已经被onStartCommand替代
发表于 2017-09-27 22:38:50 回复(0)
c和d都不是呀
发表于 2017-08-30 10:17:01 回复(0)
service的生命周期
发表于 2017-06-14 07:53:57 回复(0)
从android源代码可以看到没有onStop()方法。所以选C

public abstract class Service extends ContextWrapper implements ComponentCallbacks { public Service() { super((Context)null); throw new RuntimeException("Stub!");
    } public final Application getApplication() { throw new RuntimeException("Stub!");
    }  public void onCreate() { throw new RuntimeException("Stub!");
    } public void onStart(Intent intent, int startId) { throw new RuntimeException("Stub!");
    } public int onStartCommand(Intent intent, int flags, int startId) { throw new RuntimeException("Stub!");
    } public void onDestroy() { throw new RuntimeException("Stub!");
    } public void onConfigurationChanged(Configuration newConfig) { throw new RuntimeException("Stub!");
    } public void onLowMemory() { throw new RuntimeException("Stub!");
    } public abstract IBinder onBind(Intent var1); public boolean onUnbind(Intent intent) { throw new RuntimeException("Stub!");
    } public void onRebind(Intent intent) { throw new RuntimeException("Stub!");
    } public final void stopSelf() { throw new RuntimeException("Stub!");
    } public final void stopSelf(int startId) { throw new RuntimeException("Stub!");
    } public final boolean stopSelfResult(int startId) { throw new RuntimeException("Stub!");
    } public final void setForeground(boolean isForeground) { throw new RuntimeException("Stub!");
    } public final void startForeground(int id, Notification notification) { throw new RuntimeException("Stub!");
    } public final void stopForeground(boolean removeNotification) { throw new RuntimeException("Stub!");
    } protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { throw new RuntimeException("Stub!");
    } protected void finalize() throws Throwable { throw new RuntimeException("Stub!");
    }
}


编辑于 2017-05-19 20:29:43 回复(0)

C

发表于 2017-04-03 16:20:13 回复(0)
我觉得是C,因为service只继承了onCreate()和onDestroy()、onStart()这三个方法,当我们第一次启动service的时候调用了onCreate()和onStart(),这个两个方法,当停止service的时候,执行onDestroy()这个方法,而不是执行onStop()什么的。一旦我们之前启动了service,当我们再次执行service的时候,不是执行onCreate()这个方法,而是直接执行onstart().
发表于 2017-01-03 22:56:05 回复(0)
c
发表于 2017-01-03 20:00:55 回复(0)