分享

android如何获取系统开机时间

 一楠tech 2017-10-25 发布于北京
开发过程中有时候需要获取android开机时间,这个是两年前遇到的一个问题,在此总结一下。
方法一、通过系统SystemClock接口运算
android.os.SystemClock的elapsedRealtimeNanos返回从系统开机现在的经历过的时间(单位:纳秒),包括系统休眠待机时间。所以我们可以使用当前时间减去系统开机到现在的时间计算出系统的启动时间
  1. // 返回开机时间,单位微妙  
  2. long bootTime() {  
  3.     return System.currentTimeMillis() - SystemClock.elapsedRealtimeNanos() / 1000000;  
  4. }  
方法二、通过读取/proc/stat文件获取系统开机时间
方法一获取系统开机时间非常方便,但是有一个缺点必须在Android 4.2以上才能支持该接口,也就是说在低平台调用接口会应为找不到该函数而闪退。

不过没关系,我们可以使用linux层提供的信息。
关于 /proc/stat文件: stat文件记录了当前系统常有状态,例如cpu时钟,内存页,开机时间等等。其中开机时间是btime字段后面的数值单位是秒
以下是本人手机设备的 /proc/stat内容(华为, Android6.0系统)
[plain] view plain copy
  1. shell@HWGRA:/proc $ cat stat  
  2. cpu  1840729 131539 1145597 10753491 13057 57 45011 0 0 0  
  3. cpu0 246543 17369 249206 7219225 3792 11 32119 0 0 0  
  4. cpu1 282309 19254 210287 545809 2092 39 4639 0 0 0  
  5. cpu2 279958 19834 209812 547156 2380 3 2743 0 0 0  
  6. cpu3 276153 20196 203136 548855 2257 4 1970 0 0 0  
  7. cpu4 173666 14209 67996 502766 1131 0 1414 0 0 0  
  8. cpu5 205458 13675 70375 448196 528 0 703 0 0 0  
  9. cpu6 210211 13822 70829 449078 430 0 710 0 0 0  
  10. cpu7 166431 13180 63956 492406 447 0 713 0 0 0  
  11. intr 164085213 0 0 0 0 341 0 0 0 167 167 0 0 0 4 3 0 0 0 0 0 0 0 0 0 0 0 0 19263077 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 50348 0 0 0 0 0 0 6615253 0 0 0 0 0 0 68541 0 0 620 0 0 0 0 3304 0 0 0 0 0 0 0 0 177234 26390930 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 53695 0 0 32981493 9285925 2391853 2011 0 0 0 0 0 0 0 0 0 485 0 0 0 0 0 1480860 100 2118249 0 0 0 0 5 998345 0 196 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 991829 51562 0 0 0 131664 0 918 0 429104 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 69486 31005 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 1 21 0 0 0 0 0 0 286338 0 0 0 8 1 0 143155 1  
  12. ctxt 197939068  
  13. btime 1501803547  
  14. processes 238588  
  15. procs_running 2  
  16. procs_blocked 0  
  17. softirq 78287404 12496 14322913 16214 439295 11578 11578 45998374 3558439 4601 13911916  
可以看到btime字段
btime 1501803547
表示从1970-01-01 00:00:00 +0000起始的时间,单位:秒
可以用java轻松实现,本博客使用jni实现。
主要实现方案
头文件:android_system_utils .h
  1. #ifndef ANDROID_SYSTEM_UTILS_H  
  2. #define ANDROID_SYSTEM_UTILS_H  
  3.   
  4. #include <cstddef>  
  5.   
  6. class android_system_utils {  
  7. public:  
  8.     /** 
  9.      * get android system boot time(unit second). 
  10.      * time since 1970-01-01 00:00:00 +0000 (UTC). 
  11.      */  
  12.     static long getBootTime();  
  13.   
  14. private:  
  15.     static long parseBootTime(const char* text, size_t size);  
  16. };  
  17.   
  18. #endif  
源文件:android_system_utils.cpp
  1. #include "android_system_utils.h"  
  2.   
  3. #include <cstdio>  
  4. #include <cstdlib>  
  5. #include <fcntl.h>  
  6. #include <cctype>  
  7.   
  8. #include "common.h"  
  9. #include "file_utils.h"  
  10.   
  11. static char TAG[] = "android_system_utils";  
  12.   
  13. long android_system_utils::getBootTime() {  
  14.     const char filepath[] = "/proc/stat";  
  15.   
  16.     if (!file_utils::isExists(filepath)) {  
  17.         return -1;  
  18.     }  
  19.   
  20.     int fd = open(filepath, O_RDONLY);  
  21.     if (-1 == (fd)) {  
  22.         lerror(TAG, "getBootTime. open: %s file failed. errno: %d, error: %s", errno, strerror(errno));  
  23.         return -1;  
  24.     }  
  25.     errno = 0;  
  26.     char storebuf[128] = {0}; // 128bit is enough as we only concern about btime text.  
  27.     char buf[2] = {0};  
  28.     ssize_t len = 0;  
  29.     long ret = -1;  
  30.     do {  
  31.         memset(buf, 0, sizeof(buf));  
  32.         if ((len = safe_read(fd, buf, 1)) > 0) { // only read on character.  
  33.             if ('\n' == buf[0]) { // match a new line.  
  34.                 if (-1 != (ret = parseBootTime(storebuf, strlen(storebuf)))) {  
  35.                     break;  
  36.                 }  
  37.                 memset(storebuf, 0, sizeof(storebuf));  
  38.             } else if (strlen(storebuf) < sizeof(storebuf) - 1) { // fill a line data.  
  39.                 strcat(storebuf, buf);  
  40.             }  
  41.         }  
  42.     } while (len > 0);  
  43.     if (-1 == len) {  
  44.         lerror(TAG, "getBootTime. read error. errno: %d, error: %s", errno, strerror(errno));  
  45.     }  
  46.   
  47.     close(fd);  
  48.   
  49.     return ret;  
  50. };  
  51.   
  52. long android_system_utils::parseBootTime(const char *text, size_t size) {  
  53.     const char btime[] = "btime";  
  54.     const char *find = strstr(text, btime);  
  55.     if (NULL == find) {  
  56.         return -1;  
  57.     }  
  58.   
  59.     char c[2] = {0};  
  60.     char buf[64] = {0};  
  61.     for (size_t i = 0; i < size; i++) {  
  62.         c[0] = *(text + i);  
  63.         if (isdigit(c[0])) { // fill buf with digital.  
  64.             strcat(buf, c);  
  65.         }  
  66.     }  
  67.     if (0 == strlen(buf)) {  
  68.         return -1;  
  69.     }  
  70.   
  71.     return atol(buf);  
  72. }  
三、总结
虽然使用stat文件也能够获取开机时间,但是精度只能到达秒,还是没有系统SystemClock.elapsedRealtimeNanos的精度搞。因此我们折中处理,在android4.2高版本使用系统API接口,低版本使用stat数值接口,即保证兼容性又能够发挥高版本的精度问题。
DeviceHelper.java
  1. package org.penny;  
  2.   
  3. import android.os.Build;  
  4. import android.os.SystemClock;  
  5.   
  6. /** 
  7.  * Created by lucky on 05/08/2017. 
  8.  */  
  9.   
  10. public class DeviceHelper {  
  11.     static {  
  12.         System.loadLibrary("Pannel");  
  13.     }  
  14.   
  15.     /** 
  16.      * get android system boot time(unit milli second). 
  17.      * time since 1970-01-01 00:00:00 +0000 (UTC). 
  18.      */  
  19.     public static long getBootTime() {  
  20.         if (Build.VERSION.SDK_INT < 17) {  
  21.             return native_getBootTime() * 1000;  
  22.         }  
  23.         return System.currentTimeMillis() - SystemClock.elapsedRealtimeNanos() / 1000000;  
  24.     }  
  25.   
  26.     /** 
  27.      * get android system boot time(unit second). 
  28.      * time since 1970-01-01 00:00:00 +0000 (UTC). 
  29.      */  
  30.     public static native long native_getBootTime();  
  31. }  
DeviceHelperTest.java
  1. package org.penny;  
  2.   
  3. import android.os.Build;  
  4. import android.support.test.runner.AndroidJUnit4;  
  5. import android.util.Log;  
  6.   
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9.   
  10. import static org.junit.Assert.assertEquals;  
  11.   
  12. /** 
  13.  * Created by lucky on 05/08/2017. 
  14.  */  
  15.   
  16. @RunWith(AndroidJUnit4.class)  
  17. public class DeviceHelperTest {  
  18.     private static final String TAG = DeviceHelperTest.class.getSimpleName();  
  19.   
  20.     @Test  
  21.     public void testNativeGetBootTime() {  
  22.         Log.d(TAG, "testNativeGetBootTime...");  
  23.         Log.d(TAG, "boot time: " + DeviceHelper.native_getBootTime());  
  24.     }  
  25.   
  26.     @Test  
  27.     public void testGetBootTime() {  
  28.         Log.d(TAG, "testGetBootTime...");  
  29.         Log.d(TAG, "boot time: " + DeviceHelper.getBootTime());  
  30.   
  31.         if (Build.VERSION.SDK_INT >= 17) {  
  32.             assertEquals(DeviceHelper.native_getBootTime(), DeviceHelper.getBootTime() / 1000);  
  33.         }  
  34.     }  
  35. }  
测试用例结果
--------- beginning of main
08-05 21:36:35.114 11597 11612 D DeviceHelperTest: testGetBootTime...
08-05 21:36:35.119 11597 11612 D DeviceHelperTest: boot time: 1501803547791
08-05 21:36:35.126 11597 11612 D DeviceHelperTest: testNativeGetBootTime...
08-05 21:36:35.128 11597 11612 D DeviceHelperTest: boot time: 1501803547
因为写博客的时候顺便跑过代码,所以就把代码发到github上面
https://github.com/FreeJack007/Pannel

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多