在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啊。

更多相关文章

  1. 移动开发:Android官方提供的支持不同屏幕大小的全部方法
  2. AndroidStudio中使用Kotlin--实现接口方法
  3. Android(安卓)架构组件之 Lifecycle 使用详解
  4. Android(安卓)- base - SQLite
  5. 仿QQ设置字体大小自定义SeekBar
  6. android sqlite 操作:rawQuery and execSQL
  7. Handler机制原理
  8. android反混淆工具
  9. Android(安卓)getView中的position问题解决方案

随机推荐

  1. Android(安卓)开发之Android(安卓)应用程
  2. Android(安卓)Studio 开发,找不到android.
  3. Android 解屏代码
  4. Android 4.0 HttpUrlConnection的getInpu
  5. Android 复习笔记之图解Activity的4种加
  6. android新建项目报关于progurad.xml不存
  7. android 输入框自动匹配-AutoCompleteTex
  8. android 自动更新网址保存
  9. Android(安卓)TV RecyclerView 焦点处理
  10. android View 绘制完成监听