ONSTART()和onStartCommand()仍称在2.0及更高版本 [英] onStart() and onStartCommand() still called in 2.0 and higher

查看:232
本文介绍了ONSTART()和onStartCommand()仍称在2.0及更高版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据博客员额和文档<一href="http://developer.android.com/reference/android/app/Service.html#onStartCommand%28android.content.Intent,%20int,%20int%29"相对=nofollow> onStartCommand() 如果你有一个服务,你应该实现的OnStart()和onStartCommand(),并在2.0和更高版本onStartCommand()将被调用。看来,这是情况并非如此,在我的服务都是被调用。这是一个问题,因为它试图做的工作了两次,所以我不得不添加在ONSTART检查()不做任何事情,如果OS版本是&LT; 2.0。这似乎是一个黑客和一个错误。任何人的经验,这还是我也许有什么不对?我将和从样品粘贴的code正确。

  @覆盖
公众诠释onStartCommand(意向意图,诠释标志,诠释startId){
  Util.log(mCtxAlerterService,onStartCommand()叫做);
  handleStart(意向);
  返回super.onStartCommand(意向,标志,startId);
}

公共无效ONSTART(意向意图,诠释startId){
    Util.log(mCtxAlerterService,ONSTART()叫做);
    handleStart(意向);
    super.onStart(意向,startId);
}
 

解决方案

在这一篇博客文章中,基implementantions ONSTART onStartCommand 不叫。 pressumably,其中之一是调用其他的。

According to this blog post and the documentation of onStartCommand() if you have a Service you should implement onStart() and onStartCommand() and in 2.0 and higher only onStartCommand() will be called. It seems that this is not the case and in my Service BOTH are being called. This was a problem as it was trying to do the work twice, so I had to add a check in onStart() to not do anything if the OS version was < 2.0. This seems like a hack and a bug. Anyone else experience this or do I maybe have something wrong? I cut and pasted the code right from the sample.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  Util.log(mCtx, "AlerterService", "onStartCommand() called");
  handleStart(intent);
  return super.onStartCommand(intent, flags, startId);
}

public void onStart(Intent intent, int startId) {
    Util.log(mCtx, "AlerterService", "onStart() called");       
    handleStart(intent);
    super.onStart(intent, startId);
}

解决方案

On that blog post, the base implementantions of onStart and onStartCommand are not called. Pressumably, one of them is calling the other.

这篇关于ONSTART()和onStartCommand()仍称在2.0及更高版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆