iOS 9已下的获取APP进程信息
2016-04-01 17:31
459 查看
- (NSDictionary *)getAppInfo:(NSString *)exec withBundleID:(NSString *)bundle { if ([exec isKindOfClass:[NSTimer class]]) { exec = [(NSTimer *)exec userInfo]; } NSDictionary * ret = nil; int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; u_int miblen = 4; size_t size = 0; int st = 0; struct kinfo_proc * process = NULL; struct kinfo_proc * newprocess = NULL; sysctl(mib, miblen, NULL, &size, NULL, 0); do { size += size / 10; newprocess = realloc(process, size); if (!newprocess) { if (process) { free(process); } return ret; } 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) { unsigned long nprocess = size / sizeof(struct kinfo_proc); if (nprocess) { for (long i = nprocess - 1; i >= 0; i --) { NSString * procExecName = [NSString stringWithCString:process[i].kp_proc.p_comm encoding:NSUTF8StringEncoding]; NSString * procBundleId = bundle; long timeInterval = process[i].kp_proc.p_un.__p_starttime.tv_sec; // !!!:时间转化 NSDateFormatter * format = [[NSDateFormatter alloc] init]; [format setDateStyle:NSDateFormatterMediumStyle]; [format setTimeStyle:NSDateFormatterShortStyle]; [format setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS'Z'"]; NSTimeZone * timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"]; [format setTimeZone:timeZone]; NSDate * oldTime = [NSDate dateWithTimeIntervalSince1970:timeInterval]; NSDate * nowTime = [NSDate date]; NSString * procSTime = [format stringFromDate:oldTime]; NSString * procETime = [format stringFromDate:nowTime]; int t = [nowTime timeIntervalSince1970] - timeInterval; NSString * procUTime = [[NSNumber numberWithInt:t] stringValue]; // bundleid, execName, stime, etime,utime NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; [dic setObject:procExecName forKey:@"KPROCEXECNAME"]; [dic setValue:procBundleId forKey:@"KPROBUNDLEID"]; [dic setValue:procSTime forKey:@"KPROCSTIME"]; [dic setValue:procETime forKey:@"kPROCETIME"]; [dic setValue:procUTime forKey:@"KPROCUTIME"]; ret = [NSDictionary dictionaryWithDictionary:dic]; } free(process); return ret; } } } free(process); return ret; }
需要的头文件为:
#import <sys/sysctl.h>
相关文章推荐
- Swift 学习记录(4)
- iOS 解析json串
- Android fragment 重叠问题的解决方法
- 在Android Studio中有六种依赖
- iOS多线程
- nrf51822微信---AES加密
- iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
- 【Android 2D 游戏开发(4)】——俄罗斯方块(手势操作)
- Android NestedScrolling与分发机制 二
- iOS中block 静态全局局部变量block变量
- Android 添加页面切换动画, 避免冷启动
- Android 使用DrawerLayout快速实现侧滑菜单
- [iOS] 报错:Not a PNG file...
- android开源的酷炫的交互动画和视觉效果:Interactive-animation
- 安卓开发——androidstudio自动导包(导入全部包)
- Android 看IntentService源码延伸
- Swift 笔记
- Android静默安装实现方案,仿360手机助手秒装和智能安装功能
- 股票输入软键盘,android初探自定义软键盘
- ios 添加pch文件