简体   繁体   中英

Running apps in background or active?

Actually i am working on iPhone app.I wanted to trac the sate of application or number of application which are in background same for number of apps which are in active state. Can you have any idea how to trac the task.As i think it's related to task manager. Please give me some pointer. Thanks

I have spent tone of time when was trying to do this. But no result.

The best result that I was able to get is getting list of running application both active and background (actually list of running process):

-(void) listOfProcesses {
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t miblen = 4;

size_t size;
sysctl(mib, miblen, NULL, &size, NULL, 0);
int st;

struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;

do {       
    size += size / 10;
    newprocess = realloc(process, size);
    if (!newprocess){
        if (process){
            free(process);
        }
    }
    process = newprocess;
    st = sysctl(mib, miblen, process, &size, NULL, 0);
} while (st == -1 && errno == ENOMEM);

if (st == 0){
    if (size % sizeof(struct kinfo_proc) == 0){
        int nprocess = size / sizeof(struct kinfo_proc);
        if (nprocess){              
            for (int i = nprocess - 1; i >= 0; i--){
                NSLog(@"Pid: %d, Process name: %@",process[i].kp_proc.p_pid,process[i].kp_proc.p_comm);
            }
            free(process);
        }
    }
}

}

In this list there are several system process, but there is no problem to sort them.

For mo information please see this manual:

This is list of Unix System call. This may be helpful.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM