찾다

 >  Q&A  >  본문

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

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

ringa_leeringa_lee2772일 전624

모든 응답(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
  • 취소회신하다