Service的onStartCommand方法的疑问
16lz
2021-01-26
在Mars的Android视频的25集Service中,继承了Service类。依视频写代码,却发现没有 public int onStartCommand ( Intent intent, int flags, int startId) 可以覆写。查了一下API文档,在示例代码的onStart方法注释之中有这样两句话:
// This is the old onStart method that will be called on the pre-2.0// platform. On 2.0 or later we override onStartCommand() so this// method will not be called.
看到这里才恍然大悟,原来onStart方法是在Android2.0之前的平台使用的.在2.0及其之后,则需重写onStartCommand方法,同时,旧的onStart方法则不会再被直接调用(外部调用onStartCommand,而onStartCommand里会再调用 onStart。在2.0之后,推荐覆盖onStartCommand方法,而为了向前兼容,在onStartCommand依然会调用onStart方法。感谢 swordlife1986的指正)。
在文档中,onStart方法的解释是以下两句话:
This method is deprecated.Implement onStartCommand(Intent, int, int) instead.
意思就是不赞成使该方法。该方法的实现已经被onStartCommand所代替。
看来还是以前的惯性思维啊。之前建工程的时候一直都是选以1.6为最低的平台,这次也选了这个,难怪会在重写的时候没有这个方法了。另外,看到视频后面,在AndroidManifest.xml中果然看到了这样一句:
<use-sdk android:minSdkVersion="7" />
最低平台为7,貌似就是Android2.1啊。
更多相关文章
- 移动开发:Android官方提供的支持不同屏幕大小的全部方法
- AndroidStudio中使用Kotlin--实现接口方法
- Android(安卓)架构组件之 Lifecycle 使用详解
- Android(安卓)- base - SQLite
- 仿QQ设置字体大小自定义SeekBar
- android sqlite 操作:rawQuery and execSQL
- Handler机制原理
- android反混淆工具
- Android(安卓)getView中的position问题解决方案