adb在背景上运行应用时挂起

问题描述 投票:1回答:1

我有一个应该在后台运行的程序。我试图使用adb触发它并获得以下行为:

adb shell "app &"
adb shell ps | grep myapp

表明该应用未运行。

adb shell
$app &
$exit

终端没有响应的结果。在杀死adb进程后,终端被释放然后当我检查时:

adb shell ps | grep myapp

我看到该应用程序正在后台运行。

有人可以解释这种行为吗?如何从命令行运行应用程序并让它通过cli在后台运行?

Android Debug Bridge version 1.0.32 
Revision 9e28ac08b3ed-android
android shell adb command-line-interface
1个回答
1
投票

您的app是与ADB连接时生成的shell的子项。当您退出shell时,您的应用程序将被终止,因为shell已被终止。您应该从shell分离您的应用程序:

使用nohup

adb shell "nohup app &"

使用daemonize(在某些Android系统上可用):

adb shell daemonize app
adb shell toybox daemonize app

如果你遇到麻烦(像我一样)nohup会挂起adb shell命令,如果daemonize不可用,你可以自己在C中编程,如下所示:

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <unistd.h>

int main(int argc, char ** argv)
{
  int pid = fork();
  if (pid > 0) {
    printf("Father dies\n");
    return 0;
  }

  /* redirect_fds(): redirect stdin, stdout, and stderr to /dev/NULL */
  (void) close(0);
  (void) close(1);
  (void) close(2);
  (void) dup(0);
  (void) dup(0);

  while (1)
  {
    printf("Child runs silently\n");
    sleep(1);
    /* Do long stuff in backgroudn here */
  }
  return 0; 
}
© www.soinside.com 2019 - 2024. All rights reserved.