【问题标题】:how to use variables of another class in the another logger class如何在另一个记录器类中使用另一个类的变量
【发布时间】:2017-12-19 05:38:05
【问题描述】:

我有一个 android 应用程序,其中包含各种 android 库模块,它们指定应用程序执行的活动/事件。现在我需要构建一个 eventlog android库,用于记录应用程序中发生的各种事件,如登录、注销等。

  public void Loginlog(int elapsedMs, String UserId, int   failedPasscodeAttempts, int successfulattempts) {
    createJson(TAG, "elapsedMs: "  + getTimeString(elapsedMs) + " UserId : " + UserId +  "failedattempts : " + failedPasscodeAttempts +  "Successfulattempts : "  +successfulattempts +  " attempts: " + totalattempts);
}

failedattempts、successfulattempts、尝试等变量在不同的android模块的另一个登录类中使用。所以这是使用不同类变量的正确方法。

那我该怎么做,请给点建议。

【问题讨论】:

  • 使用共享首选项存储变量或使用接口

标签: android json logging


【解决方案1】:

你可以制作静态变量

像这样创建新类

public class Utils
{
   public static String DATA="NILU";
}

当您想使用该数据时,请以这种方式使用

String str = Utils.DATA;
Utils.DATA = "NEW DATA";

您可以将值存储在SharedPreferences

用于访问和修改 getSharedPreferences(String, int) 返回的首选项数据的接口。对于任何特定的首选项集,所有客户端都共享一个此类的单个实例。

【讨论】:

  • 我们需要同时创建 Json 和 StringtoJson 类,然后我们可以在任何类 ryt 中扩展?
猜你喜欢
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多