Service的onStartCommand方法的疑问

来源:互联网 发布:js冒泡排序 从小到大 编辑:程序博客网 时间:2024/04/30 23:04
在Mars的Android视频的25集Service中,继承了Service类。依视频写代码,却发现没有public int onStartCommand (Intent intent, int flags, int startId)可以覆写。查了一下API文档,在示例代码的onStart方法注释之中有这样两句话:
[java] view plaincopy
  1. // This is the old onStart method that will be called on the pre-2.0  
  2. // platform. On 2.0 or later we override onStartCommand() so this  
  3. // method will not be called.  

      看到这里才恍然大悟,原来onStart方法是在Android2.0之前的平台使用的.在2.0及其之后,则需重写onStartCommand方法,同时,旧的onStart方法则不会再被直接调用(外部调用onStartCommand,而onStartCommand里会再调用 onStart。在2.0之后,推荐覆盖onStartCommand方法,而为了向前兼容,在onStartCommand依然会调用onStart方法。感谢 swordlife1986的指正)。

      在文档中,onStart方法的解释是以下两句话:

[java] view plaincopy
  1. This method is deprecated.  
  2. Implement onStartCommand(Intent, intint) instead.  

  意思就是不赞成使该方法。该方法的实现已经被onStartCommand所代替。

  看来还是以前的惯性思维啊。之前建工程的时候一直都是选以1.6为最低的平台,这次也选了这个,难怪会在重写的时候没有这个方法了。另外,看到视频后面,在AndroidManifest.xml中果然看到了这样一句:

[html] view plaincopy
  1. <use-sdk android:minSdkVersion="7" />  

      最低平台为7,貌似就是Android2.1啊。

0 0
原创粉丝点击