xbmc 从android_main启动时的部分步骤记录

xbmc/android/Android_main.cpp
*/
extern void android_main(struct android_app* state)
{
  {
    app_dummy();
    state->inputPollSource.process = process_input;

    CEventLoop eventLoop(state);
    CXBMCApp xbmcApp(state->activity);
    if (xbmcApp.isValid())
    {
      IInputHandler inputHandler;
      eventLoop.run(xbmcApp, inputHandler);
    }
  ...
  }
  exit(0);
}

/*
xbmc/android/activity/XBMCApp.cpp
*/
CXBMCApp::CXBMCApp(ANativeActivity* nativeActivity)
  : CJNIContext(nativeActivity)
  , CJNIBroadcastReceiver("org/xbmc/xbmc/XBMCBroadcastReceiver")
  , m_wakeLock(NULL)
{
  m_activity = nativeActivity;
  m_firstrun = true;
  m_exiting=false;
  ....
}

/*
xbmc/android/activity/EventLoop.cpp
*/
CEventLoop::CEventLoop(android_app* application)
  : m_enabled(false),
    m_application(application),
    m_activityHandler(NULL), m_inputHandler(NULL)
{
  if (m_application == NULL)
    return;

  m_application->userData = this;
  m_application->onAppCmd = activityCallback;
  m_application->onInputEvent = inputCallback;
}

void CEventLoop::run(IActivityHandler &activityHandler, IInputHandler &inputHandler)
{
  int ident;
  int events;
  struct android_poll_source* source;

  m_activityHandler = &activityHandler;
  while (1)
  {
    // We will block forever waiting for events.
    while ((ident = ALooper_pollAll(-1, NULL, &events, (void**)&source)) >= 0)
    {
      // Process this event.
      if (source != NULL)
        source->process(m_application, source);
      ...
    }
  }
}

void CEventLoop::activityCallback(android_app* application, int32_t command)
{
  if (application == NULL || application->userData == NULL)
    return;

  CEventLoop& eventLoop = *((CEventLoop*)application->userData);
  eventLoop.processActivity(command);
}
void CEventLoop::processActivity(int32_t command)
{
  switch (command)
  {
    ...
    case APP_CMD_START:
      m_activityHandler->onStart();
      break;
    ...
  }
}

IActivityHandler类的onStart()是抽象方法
类CXBMCApp继承了IActivityHandler,m_activityHandler实际是xbmcApp
/*
xbmc/android/activity/XBMCApp.cpp
*/
void CXBMCApp::onStart()
{
  android_printf("%s: ", __PRETTY_FUNCTION__);
  if (!m_firstrun)
  {
    android_printf("%s: Already running, ignoring request to start", __PRETTY_FUNCTION__);
    return;
  }
  pthread_attr_t attr;
  pthread_attr_init(&attr);
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  pthread_create(&m_thread, &attr, thread_run<CXBMCApp, &CXBMCApp::run>, this);
  pthread_attr_destroy(&attr);
}
好,m_thread run方法应该是跟CXBMCApp::run绑定了

void CXBMCApp::run()
{
  SetupEnv();//设置环境
  XBMC::Context context;

  m_initialVolume = GetSystemVolume();

  CJNIIntent startIntent = getIntent();
  android_printf("XBMC Started with action: %s\n",startIntent.getAction().c_str());

  std::string filenameToPlay = GetFilenameFromIntent(startIntent);
  if (!filenameToPlay.empty())
  {
    int argc = 2;
    const char** argv = (const char**) malloc(argc*sizeof(char*));

    std::string exe_name("XBMC");
    argv[0] = exe_name.c_str();
    argv[1] = filenameToPlay.c_str();
    CAppParamParser appParamParser;
    appParamParser.Parse((const char **)argv, argc);
    free(argv);
  }

  m_firstrun=false;
  android_printf(" => running XBMC_Run...");
  try
  {
    status = XBMC_Run(true);
    android_printf(" => XBMC_Run finished with %d", status);
  }
  ...
  // If we are have not been force by Android to exit, notify its finish routine.
  // This will cause android to run through its teardown events, it calls:
  // onPause(), onLostFocus(), onDestroyWindow(), onStop(), onDestroy().
  ANativeActivity_finish(m_activity);//告诉android系统,apk启动成功
  m_exiting=true;
}





你可能感兴趣的:(android,xbmc)