gpt4 book ai didi

java - Android静态变量丢失

转载 作者:太空狗 更新时间:2023-10-29 13:55:04 25 4
gpt4 key购买 nike

我正在使用一个只有静态变量的类来存储所有常量和运行时属性。但是,最近我看到在引用这些变量时出现空值的情况。

以下是我的类定义:

public class PlayerProperties {

// Runtime but constant properties
public static int screenHeight;
public static int screenWidth;
public static String androidId;

// Static properties

// Urls
public static final String baseUrl = "http://www.blynq.in/api/player/";

public static final String registerUrlAppender = "activationKeyValid";
public static final String schedulesUrlAppender = "getScreenData";
public static final String updateUrlAppender = "updateAvailable";
public static final String tokenRegisterUrl = "fcmRegister";
public static final String mediaStatsUrl = "mediaStats";
public static final String logsUrl = "logs";
public static final String pingUrl = "ping";
public static final String screenInfoUrl = "screenInfo";

// Developer Keys
public static final String youtubeDeveloperKey = "ABCDEFGH...";

// Folder structure
public static final String mediaFolder = "player/media";
public static final String imagesFolder = "player/media/images";
public static final String videosFolder = "player/media/videos";
public static final String pdfFolder = "player/media/pdf";
public static final String gifFolder = "player/media/gif";
public static final String webFolder = "player/media/web";
public static final String othersFolder = "player/media/others";
public static final String logsFolder = "player/logs";

public static final String defaultFolder = "player/default/";
public static final String serFolder = "player/ser/";
public static final String tempFolder = "player/temp/";

// Shared Prefs Keys
public static final String ANDROID_ID_KEY = "ANDROID_ID";
public static final String MY_PREFERENCES_KEY = "MyPrefs";


// General properties
public static final String dateTimeFormatString = "ddMMyyyyHHmmss";
public static final String dateFormatString = "yyyy-MM-dd";

// Timeouts
public static final int httpPollTimeout = 20000; // in millis
public static final int pingPeriodicity = 30; // in secs
public static final int updateCheckPeriodicity = 24; // in hrs
public static final int pushEnabledPollPeriodicity = 30; // in secs
public static final int pushDisabledPollPeriodicity = 30; // in secs
public static final int statsUploadPeriodicity = 60; // in mins
public static final int logsUploadPeriodicity = 24; // in hours
public static final int cleanupPeriodicity = 24; // in hours
public static final int registrationStatusRecheckPeriod = 20000; // in millis
public static final int tokenResendToServerPeriod = 20000; // in millis

// Others
public static final int maxTextHeight = 50; // in dp
...
}

我没有在 PlayerProperties 类中存储任何引用实例,因为其中包含的所有变量都是静态的。

当我使用 PlayerProperties.androidId 引用变量 androidId 时,我有时会得到 null。

我已经在其中一项 Activity 中初始化了变量:

PlayerProperties.androidId = sharedpreferences.getString(PlayerProperties.ANDROID_ID_KEY, String.valueOf(UUID.randomUUID()));
  1. 我怀疑垃圾收集器是在中间被 android 踢掉的。如果 gc 启动,它会关闭我所有运行时初始化的静态变量吗?

  2. 静态变量存储在哪个内存段?

  3. 如果不是,还有什么问题?

额外详细信息:我的应用配置为在启动时自动启动。我仅在低端处理器和应用程序在启动时自动触发时面临上述问题。

最佳答案

关于上述问题,请注意以下几点:

  1. 垃圾收集器只删除未引用的对象。静态变量只有在运行时从 JVM 卸载时才会丢失它们的值。

  2. 应用程序通过 Activity 启动时会发生初始化。如果有任何服务/广播接收器正在访问其他类中的方法并使用这些 ID,并且此时应用程序尚未运行,则未初始化的值将被引用为默认值。如果是字符串 - 它为空。

  3. 与低端处理器无关,容易经常责怪它们,但 JVM 足够强大。

关于java - Android静态变量丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40595803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com