如何在不使用静态变量中的硬编码值的情况下初始化 Android C2DM 的发件人 ID?
我正在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们有同样的问题。
我们通过使用assets文件夹下的properties文件解决了这个问题。
可以使用静态配置帮助器类静态加载属性。
第一次加载应用程序时,可以使用应用程序上下文获取属性文件。
例如:
1.初始化静态配置
公共无效onCreate(捆绑保存实例状态){
super.onCreate(savedInstanceState);
myConfig = new WLConfig(getApplication());
}
然后在配置类中使用:
myProperties.load(context.getAssets().open("myclient.properties"));
并获取发件人电子邮件:
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());
}
Then use in the configuration class:
myProperties.load(context.getAssets().open("myclient.properties"));
And get the sender email:
return myProperties.getProperty(WL_C2DM_SENDER)
“多个同时运行的应用程序”每个都有自己的静态变量副本,因为每个应用程序都在自己的进程中运行。
"Multiple concurrently running apps" each have their own copy of the static variable, since each runs in its own process.