【问题标题】:confuse about Android IntentService [duplicate]对Android IntentService感到困惑[重复]
【发布时间】:2015-12-18 06:34:45
【问题描述】:

下面是IntentService源码的两个方法

@Override
public void onStart(Intent intent, int startId) {
    Message msg = mServiceHandler.obtainMessage();
    msg.arg1 = startId;
    msg.obj = intent;
    mServiceHandler.sendMessage(msg);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    onStart(intent, startId);
    return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}

当我用debug模式启动intentservice的子类时,我发现,它先调用onstart方法,然后执行onStartCommand方法,但是在onStartCommand方法中,它没有调用“onStart(intent, startId); "

看到源码,我认为应该是先调用onStartCommon方法,然后在onStartCommon方法中执行onStart方法,结果如上,我搞糊涂了,有人帮我吗?谢谢

【问题讨论】:

    标签: android android-intentservice


    【解决方案1】:

    当我阅读this document时,我写了一个名为“MyService”的新类,它扩展了Service,并重写了onStart方法和onStartCommand方法,如下所示:

    @Override
    public void onStart(Intent intent, int startId) {
        Log.e(TAG,"onStart");
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG,"onStartCommand");
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
        ........
    }
    

    在我启动MyService后,它只是调用了onStartCommand方法,但永远不会调用onStart方法!所以,我认为,因为我在sdk中调试android源,而不是我项目中的源,所以调试不正确,但我仍然拒绝IntentService.java中的源,onStartCommand方法仍然调用onStart (intent, startId);

    如果你有什么建议,请告诉我!谢谢!

    【讨论】:

      猜你喜欢
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2018-08-15
      • 2016-02-21
      • 2023-03-08
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多