如何在不使用静态变量中的硬编码值的情况下初始化 Android C2DM 的发件人 ID?

发布于 2024-09-28 04:50:24 字数 940 浏览 2 评论 0原文

我正在将 Android C2DM 添加到 Android 库项目中。我从 JumpNotecom.google.android.c2dm 软件包 > 和 Chrome 到手机。为了使用此包,您必须对 C2DMBaseReceiver 服务,它将发送者 ID 作为其构造函数的参数。在 JumpNote 中,该参数是使用配置类中的硬编码静态变量初始化的。然而,在一个 Android 库项目中,它可能被多个并发运行的应用程序使用,我认为我不能使用硬编码的静态变量(也就是说,我相信当/如果多个应用程序试图使用它时,它可能会导致问题)访问/修改静态变量)。

我试图想出一种在不使用静态变量的情况下初始化发件人 ID 的方法,但到目前为止我很困惑。

显而易见的解决方案是使用清单或资源字符串或两者的组合。例如,在 strings.xml 中,我可能有一个“ac2dmSender”字符串,可以在 C2DMReceiver 服务声明的元数据子级中访问该字符串在清单中。但是,似乎您无法从静态上下文中获取对 PackageManager 或 ResourceManager 的引用,因此我无法以将元数据传递给 C2DMBaseReceiver 的构造函数的方式检索元数据。

请让我知道我错过了一些东西!提前致谢。

I'm adding Android C2DM to a Android library project. I started with the com.google.android.c2dm package that is included with JumpNote and Chrome To Phone. In order to use this package, you have to subclass the C2DMBaseReceiver service which takes the Sender Id as an argument to it's constructor. In JumpNote, this argument is initialized using a hard-coded static variable in a config class. However, in an Android library project, which may be used by multiple concurrently running apps I don't think I can use a hard-coded static variable (that is, I believe it could lead to problems when/if multiple apps are trying to access/modify the static variable).

I tried to think of a way to initialize the Sender Id without using a static variable and am stumped so far.

The obvious solution would be to use the Manifest or a Resource string or a combination of the 2. For example, in strings.xml I might have a "ac2dmSender" string, which is accessed in a meta-data child of the C2DMReceiver service declaration in the manifest. However, it seems that you cannot get a reference to the PackageManager or ResourceManager from a static context, so there is no way for me to then retrieve the meta data in such a way as to pass it in to the constructor of C2DMBaseReceiver.

Please let me know that I'm missing something! Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁拥 2024-10-05 04:50:24

我们有同样的问题。
我们通过使用assets文件夹下的properties文件解决了这个问题。
可以使用静态配置帮助器类静态加载属性。
第一次加载应用程序时,可以使用应用程序上下文获取属性文件。

例如:
1.初始化静态配置
公共无效onCreate(捆绑保存实例状态){
super.onCreate(savedInstanceState);
myConfig = new WLConfig(getApplication());
}

  1. 然后在配置类中使用:
    myProperties.load(context.getAssets().open("myclient.properties"));

  2. 并获取发件人电子邮件:
    return myProperties.getProperty(WL_C2DM_SENDER)

We had same problem.
We solved it by using properties file under the assets folder.
Can load the properties staticly by using static configuration helper class.
On the first time the application is loaded can get the properties file using the Application context.

for example:
1. init the static configuration
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myConfig = new WLConfig(getApplication());
}

  1. Then use in the configuration class:
    myProperties.load(context.getAssets().open("myclient.properties"));

  2. And get the sender email:
    return myProperties.getProperty(WL_C2DM_SENDER)

别忘他 2024-10-05 04:50:24

但是,在一个 Android 库项目中,它可能被多个并发运行的应用程序使用,我认为我不能使用硬编码的静态变量(也就是说,我相信当/如果多个应用程序同时运行时,它可能会导致问题)正在尝试访问/修改静态变量)。

“多个同时运行的应用程序”每个都有自己的静态变量副本,因为每个应用程序都在自己的进程中运行。

However, in an Android library project, which may be used by multiple concurrently running apps I don't think I can use a hard-coded static variable (that is, I believe it could lead to problems when/if multiple apps are trying to access/modify the static variable).

"Multiple concurrently running apps" each have their own copy of the static variable, since each runs in its own process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文