【问题标题】:adb hangs when running app on background在后台运行应用程序时 adb 挂起
【发布时间】:2017-02-07 15:01:14
【问题描述】:

我有一个应该在后台运行的程序。 我正在尝试使用 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】:

    您的应用是与 ADB 连接时生成的 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; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多