开发者

How can I initialize the Sender ID for Android C2DM without using a hard-coded value in a static variable?

I'm adding Android C2DM to a Android library project. I st开发者_Python百科arted 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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜