私は1つのアプリケーションを開発しています。その中で、バックグラウンドで実行されているアプリケーションのリストを取得するための以下のコードを記述しました。
- (NSArray *)runningProcesses {
@try {
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t miblen = 4;
size_t size;
int st= sysctl(mib, miblen, NULL, &size, NULL, 0);
struct kinfo_proc * process = NULL;
struct kinfo_proc * newprocess = NULL;
do {
size += size/ 10;
newprocess = realloc(process, size);
if (!newprocess){
if (process){
free(process);
}
return nil;
}
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){
NSMutableArray *array = [[NSMutableArray alloc]init];
BOOL Found=NO;
for (int i = nprocess - 1; i >= 0; i--){
NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid];
NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm];
[array addObject:processName];
[processID release];
[processName release];
//[array addObject:dict];
//[dict release];
}
free(process);
return [array autorelease];
}
}
}
return nil;
}
@catch (NSException *ex)
{
NSLog(@"Exception is %@",ex);
return nil;
}
}
アプリ情報を提供します。たとえば、templerun、skype、Angrybirdsなどのアプリケーション情報を提供します。このコードを実行すると、アプリリストはskypeのみになりますが、残りのアプリはモバイルバックグラウンドで実行されているアプリに表示されます。リスト。だから、モバイルバックグラウンドの実行中のアプリケーションリストからすべての実行中のアプリのリストを取得する方法を教えてください。