是静态的变量真正的"全球" (系统级)Android中? [英] Are static variable truly "global" (system-wide) in Android?

查看:173
本文介绍了是静态的变量真正的"全球" (系统级)Android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一开始就快速的注意,以避免错误重复:有吨的问题在这里什么时候静态变量被清除多久他们住。这不是我要问这里。

如果我有一个程序的PC上一个静态变量,我启动程序的两个不同的副本,则每个副本通常会在自己的沙箱与它的​​静态变量自己的私有值运行。因此,他们不是全系统的全球(不知道这是很好的术语在这里)。

If I have a static variable in a program on a PC and I launch two different copies of the program, then each copy will usually run in its own sandbox with its own private values for its static variables. So, they are not system-wide global (not sure if that is good terminology here).

有没有在Android的情况下我可以有多个实例(泛指字)围绕一个静态变量?我不认为这是可能有不同沙箱中并行运行(虽然我不能肯定这)一个活动的多个副本,但如何ConentProviders,IntentServices,或任何其他类的Andr​​oid可能会随机地从其他可能的实例流程?

Are there situations in Android where I can have multiple "instances" (word used loosely) of a static variable around? I don't think it's possible to have multiple copies of an Activity running in parallel in different sandboxes (although I'm not certain of this), but how about ConentProviders, IntentServices, or any other class that Android might randomly instantiate from potentially other processes?

或者,措辞不同,如果我有一个静态变量的类,我保证,在同一时间存在于同一设备上的类的每一个实例可以访问相同的静态变量的值?

Or, phrased differently, if I have a class with a static variable, am I guaranteed that every single instance of the class existing on the same device at the same time has access to the exact same static variable value?

推荐答案

静态字段都在同一个进程中运行的所有类访问。如果服务,应用程序,广播接收器,ContentProvider的或活动是在一个单独的进程中运行那么它就会有不同的内存空间,因此将无法看到其他进程静态字段。流程=串的 http://developer.android.com/guide/topics/manifest/receiver-element.html

Static fields are accessible to all the classes running in the same process. If the Service, Application, BroadcastReceiver, ContentProvider or Activity is running in a separate process then it will have a different memory space, thus won't see static fields from the other processes. You can force a component to run on another process by specifying its' it in the AndroidManisfest (e.g. android:process="string" http://developer.android.com/guide/topics/manifest/receiver-element.html)

这篇关于是静态的变量真正的"全球" (系统级)Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆