搜尋

首頁  >  問答  >  主體

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

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

ringa_leeringa_lee2772 天前629

全部回覆(6)我來回復

  • ringa_lee

    ringa_lee2017-04-17 14:30:08

    不是進程守護嗎?

    回覆
    0
  • 巴扎黑

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

    安卓上需註冊service,這樣可以長駐後台收發訊息,activity即介面部分,可以完全關閉。

    ios上要用官方指定的background task機制實現,官方sdk提供了即時通訊後台任務接口,可以保證app進入後台或鎖屏下不被掛起或殺掉。

    回覆
    0
  • 伊谢尔伦

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

    就我所了解的知識來說。有以下方法:
    1.註冊一大堆BroadcastReceiver。有廣播就啟動。
    2.樓上說過的守護程式。 (類似數字的全家桶)
    3.編譯executeable的arm linux程式。透過java啟動。很不容易被殺死。 (不過還是過不了小米的一鍵清理)

    回覆
    0
  • PHPz

    PHPz2017-04-17 14:30:08

    各種事件掛鉤喚醒。 。

    回覆
    0
  • 伊谢尔伦

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

    service加廣播,讓你的APP一直運作

    回覆
    0
  • PHPz

    PHPz2017-04-17 14:30:08


    Android手機(除了小米MIUI)似乎預設清理後台或卸載時都是不會關閉App啟動的C進程的.搞得我打包的PHPDroid在卸載的時候還有PHP服務進程遺留,還得額外弄了個watcher C程式來inotify事件監聽安裝目錄,一旦應用目錄被刪除就kill掉PHP進程.

    我的watcher.c程式是這樣的:

    #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);
    }

    回覆
    0
  • 取消回覆