search

Home  >  Q&A  >  body text

android - 如何让APP一直存在不被杀死?

网上是了QQ是使用了1像素页面保留在前台,让系统无法认为QQ进入后台运行而能一直存在的。 请问1像素页面如何布局,如何制作?

ringa_leeringa_lee2772 days ago631

reply all(6)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 14:30:08

    Isn’t it a process daemon?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 14:30:08

    You need to register a service on Android, so that you can send and receive messages in the background permanently. The activity is the interface part and can be completely closed.

    On iOS, the officially designated background task mechanism must be used. The official SDK provides an instant messaging background task interface, which can ensure that the app will not be suspended or killed when it enters the background or locks the screen.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:30:08

    As far as I know. There are the following methods:
    1. Register a lot of BroadcastReceivers. Start when there is a broadcast.
    2. The daemon process mentioned above. (Similar to the digital Family Bucket)
    3. Compile the executable arm linux program. Start via java. Very difficult to kill. (But I still can’t pass Xiaomi’s one-click cleanup)

    reply
    0
  • PHPz

    PHPz2017-04-17 14:30:08

    Various event hooks to wake up. .

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:30:08

    Add broadcast to service to keep your APP running

    reply
    0
  • PHPz

    PHPz2017-04-17 14:30:08


    Android phones (except Xiaomi MIUI) seem to not close the C process started by the App when clearing the background or uninstalling by default. This caused the PHPDroid I packaged to be uninstalled. Sometimes there is still a PHP service process left, and I have to create an additional watcher C program to inotify events to monitor the installation directory. Once the application directory is deleted, kill the PHP process.

    My watcher.c program is like this:

    #define _GNU_SOURCE
    #include <errno.h>
    #include <poll.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/inotify.h>
    #include <unistd.h>
    
    /*
    
    http://man7.org/linux/man-pages/man7/inotify.7.html
    http://man7.org/linux/man-pages/man3/getline.3.html
    
    arm-none-linux-gnueabi-gcc watcher.c -o watcher \
    -Os -Xlinker -static -static-libgcc --sysroot=/png/dev/android/glibc
    arm-none-linux-gnueabi-strip watcher
    xz -z -k -9 watcher
    
    gcc watcher.c -o watcher -Os -static
    strip watcher
    
    touch /home/eechen/note/watcher/auth.php
    ./watcher /home/eechen/note/watcher/auth.php >/dev/null 2>&1 &
    
    */
    
    char* get_pid(char* filename) {
        FILE *stream;
        char *line = NULL;
        size_t len = 0;
        ssize_t read;
    
        stream = fopen(filename, "r");
        if (stream == NULL)
            exit(EXIT_FAILURE);
    
        if ((read = getline(&line, &len, stream)) != -1) {
            printf("Retrieved line of length %zu : ", read);
            printf("%s", line);
        }
    
        //free(line);
        fclose(stream);
        //exit(EXIT_SUCCESS);
        return line;
    }
    
    void stop_pid(char* pid) {
        char* command = NULL;
        command =  malloc(strlen("kill -9 ") + strlen(pid) + 1);
        if (command) {
            strcpy(command, "kill -9 ");
            strcat(command, pid);
            //execl("/bin/sh", "sh", "-c", command, NULL);
            execl("/system/bin/sh", "sh", "-c", command, NULL);
        }
        exit(EXIT_SUCCESS);
    }
    
    /* Read all available inotify events from the file descriptor 'fd'.
      wd is the table of watch descriptors for the directories in argv.
      argc is the length of wd and argv.
      argv is the list of watched directories.
      Entry 0 of wd and argv is unused. */
    
    static void
    handle_events(int fd, int *wd, int argc, char* argv[], char* pid)
    {
        /* Some systems cannot read integer variables if they are not
        properly aligned. On other systems, incorrect alignment may
        decrease performance. Hence, the buffer used for reading from
        the inotify file descriptor should have the same alignment as
        struct inotify_event. */
    
        char buf[4096]
        __attribute__ ((aligned(__alignof__(struct inotify_event))));
        const struct inotify_event *event;
        int i;
        ssize_t len;
        char *ptr;
        int ret;
    
        /* Loop while events can be read from inotify file descriptor. */
    
        for (;;) {
    
            /* Read some events. */
    
            len = read(fd, buf, sizeof buf);
            if (len == -1 && errno != EAGAIN) {
                perror("read");
                exit(EXIT_FAILURE);
            }
    
            /* If the nonblocking read() found no events to read, then
            it returns -1 with errno set to EAGAIN. In that case,
            we exit the loop. */
    
            if (len <= 0)
                break;
    
            /* Loop over all events in the buffer */
    
            for (ptr = buf; ptr < buf + len; ptr += sizeof(struct inotify_event) + event->len) {
    
                event = (const struct inotify_event *) ptr;
    
                /* Print event type */
                
                if (event->mask & IN_OPEN) {
                    printf("IN_OPEN: ");
                }
                if (event->mask & IN_DELETE_SELF) {
                    stop_pid(pid);
                }
    
                /* Print the name of the watched directory */
    
                for (i = 1; i < argc; ++i) {
                    if (wd[i] == event->wd) {
                        printf("%s/", argv[i]);
                        break;
                    }
                }            
                
                /* Print the name of the file */
    
                if (event->len)
                    printf("%s", event->name);
                            
                /* Print type of filesystem object */
    
                if (event->mask & IN_ISDIR)
                    printf(" [directory]\n");
                else
                    printf(" [file]\n");
            }
        }
    }
    
    int
    main(int argc, char* argv[])
    {
        char buf;
        int fd, i, poll_num;
        int *wd;
        nfds_t nfds;
        struct pollfd fds[1];
    
        if (argc < 2) {
            printf("Usage: %s PATH [PATH ...]\n", argv[0]);
            exit(EXIT_FAILURE);
        }
    
        /* Create the file descriptor for accessing the inotify API */
    
        fd = inotify_init1(IN_NONBLOCK);
        if (fd == -1) {
            perror("inotify_init1");
            exit(EXIT_FAILURE);
        }
    
        /* Allocate memory for watch descriptors */
    
        wd = calloc(argc, sizeof(int));
        if (wd == NULL) {
            perror("calloc");
            exit(EXIT_FAILURE);
        }
        
        /* Mark directories for events */
    
        for (i = 1; i < argc; i++) {
            wd[i] = inotify_add_watch(fd, argv[i], IN_DELETE_SELF);
            if (wd[i] == -1) {
                fprintf(stderr, "Cannot watch '%s'\n", argv[i]);
                perror("inotify_add_watch");
                exit(EXIT_FAILURE);
            }
        }
    
        char *pid;
        pid = get_pid("pid");
        //getline生成的堆内存变量return后在函数外free释放
        //free(pid);
        
        /* Prepare for polling */
    
        nfds = 1;
    
        /* Inotify input */
    
        fds[0].fd = fd;
        fds[0].events = POLLIN;
    
        /* Wait for events and/or terminal input */
    
        printf("Listening for events.\n");
        
        while (1) {
            poll_num = poll(fds, nfds, -1);
            if (poll_num == -1) {
                if (errno == EINTR)
                    continue;
                perror("poll");
                exit(EXIT_FAILURE);
            }
    
            if (poll_num > 0) {
                if (fds[0].revents & POLLIN) {
    
                    /* Inotify events are available */
    
                    handle_events(fd, wd, argc, argv, pid);
                }
            }
        }
    
        printf("Listening for events stopped.\n");
    
        /* Close inotify file descriptor */
    
        close(fd);
    
        free(wd);
        exit(EXIT_SUCCESS);
    }

    reply
    0
  • Cancelreply