菜鸟初学Service

J2EE程序员 2012-08-05 06:28:54
1. onStart() ,onStartCommand() instead.

As Follows :
-------------------------------------------------

onStart()
public void onStart(Intent intent, int startId) {

}

--------------------------------------------------
onStartCommand:

public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}

--------------------------------------------------
Google API

void onStart(Intent intent, int startId)
This method is deprecated. Implement onStartCommand(Intent, int, int) instead.
onStart方式已经过时,被onStartCommand取代

int onStartCommand(Intent intent, int flags, int startId)
Called by the system every time a client explicitly starts the service by calling startService(Intent),
providing the arguments it supplied and a unique integer token representing the start request.

-------------------------------------------------
那么为何还会在onStartCommand中 调用onStart()方法?

---------------------------------------------------
他的启动顺序是 onCreate() ---> onStartCommand() ---> onStart() ,说明他却是调用了onStart()
注释掉 onStartCommand()后, 依然可以启动Service啊 !! onCreate() ---> onStart()


观2.2源码中中 onStartCommand依然调用了onStart方法啊
既然都能正确的调用 那么onStart() 为何不推荐使用呢 ?
...全文
58 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hvk687 2012-08-06
  • 打赏
  • 举报
回复
为了兼容
manoel 2012-08-06
  • 打赏
  • 举报
回复
onStartCommand是新增加的方法。以前就是onStart。

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧