简体   繁体   English

android 设备上是否有唯一的启动会话 ID 或计数?

[英]Is there a unique boot session id or count on an android device?

The app I'm writing needs to know if a ' boot session ', for want of a better term, has changed but it doesn't need to actually start at boot and I would prefer if possible not to have to use the RECEIVE_BOOT_COMPLETED permission.我正在编写的应用程序需要知道“启动会话”是否已经更改,但它不需要在启动时实际启动,如果可能的话,我宁愿不必使用RECEIVE_BOOT_COMPLETED权限.

So I was wondering if there is any device-wide boot session id or count I can query and store in my db to check against later.所以我想知道是否有任何设备范围的引导会话 ID 或计数可以查询并存储在我的数据库中以供以后检查。 I know I can get the time in milliseconds since boot but I don't think that will be useful in this case.我知道我可以在启动后以毫秒为单位获得时间,但我认为在这种情况下这不会有用。

Thanks in advance for any help.提前感谢您的帮助。

Yes, on API >= 24. You can use the BOOT_COUNT global settings variable .是的,在 API >= 24 上。您可以使用BOOT_COUNT全局设置变量 To read this, try a snippet like this:要阅读本文,请尝试这样的片段:

int boot_count = Settings.Global.getInt(getContext().getContentResolver(),
                                        Settings.Global.BOOT_COUNT);

Pre-API 24, you're stuck catching RECEIVE_BOOT_COMPLETED .在 API 24 之前,您被困住了RECEIVE_BOOT_COMPLETED

I use this code to get a unique boot ID on all versions of Android.我使用此代码在所有版本的 Android 上获取唯一的启动 ID。 For Nougat and later, I use Settings.Global.BOOT_COUNT .对于 Nougat 及更高版本,我使用Settings.Global.BOOT_COUNT For versions older than Nougat, I read a unique boot ID from "/proc/sys/kernel/random/boot_id".对于早于 Nougat 的版本,我从“/proc/sys/kernel/random/boot_id”读取了一个唯一的引导 ID。 I haven't had any issues with file access on Android 5 in my testing.在我的测试中,我在 Android 5 上的文件访问没有任何问题。 Tested on Android 5, 8, 9, and 11 on real devices with a release build of our app.在 Android 5、8、9 和 11 上使用我们应用的发布版本在真实设备上进行了测试。

This does not rely on the system clock, which can jump around or be changed by the user.这不依赖于系统时钟,系统时钟可以跳跃或由用户更改。

Thanks to this answer from @george-hilliard for the Settings.Global.BOOT_COUNT solution.感谢@george-hilliard 对Settings.Global.BOOT_COUNT解决方案的回答

String bootId = "";

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ) { // Starting in Android 7 / N(ougat) / API Level 24, we have access to `Settings.Global.BOOT_COUNT`, which increments with every boot.
  bootId = Settings.Global.getString( getApplicationContext().getContentResolver(), Settings.Global.BOOT_COUNT );
}
else { // Before Android 7 / N(ougat) / API Level 24, we need to get the boot id from the /proc/sys/kernel/random/boot_id file. Example boot_id: "691aa77e-aff4-47c2-829e-a34df426c7e7".
 File bootIdFile = new File( "/proc/sys/kernel/random/boot_id" );

  if ( bootIdFile.exists() ) {
    FileInputStream inputStream = null;

    try {
      inputStream           = new FileInputStream( bootIdFile );
      BufferedReader reader = new BufferedReader( new InputStreamReader( inputStream ) );
      bootId                = reader.readLine().trim(); // Read first line of file to get the boot id and remove any leading and trailing spaces.
    }
    catch ( FileNotFoundException error ) {
      error.printStackTrace();
    }
    catch ( IOException error ) {
      error.printStackTrace();
    }
    finally {
      if ( inputStream != null ) try { inputStream.close(); } catch (IOException error ) { error.printStackTrace();; }
    }
  }
}

if ( bootId == "" ) {
  Log.e( "UniqueTag", "Could not retrieve boot ID. Build.VERSION.SDK_INT: " + Build.VERSION.SDK_INT );
}

Why not store the (absolute) date and time of the last boot in your database?为什么不在数据库中存储上次启动的(绝对)日期和时间? You can compute it easily using the time since boot, and given that a boot takes much longer than a millisecond it should be fairly accurate.您可以使用自启动以来的时间轻松计算它,并且考虑到启动时间比一毫秒长得多,它应该是相当准确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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