我可以有一个多进程全局变量吗?
我在使用涉及 2 个进程的应用程序时遇到了真正的问题。一个进程执行服务 (p1),另一个进程执行 GUI (p2)。
我在 p2 中有一个类,它实现了自定义内存管理(及其静态)的对象(iThing)的使用。它必须像这样,因为 Android 操作系统实现了随时销毁视图。
public class Connections{
public static int iGlobalCounter=0;
public static Object iThing;
public static void start(){
iGlobalCounter++;
Log.d("PROCESS", "UP: "+iGlobalCounter);
if (iGlobalCounter<=1){
//Create the object "iThing"
}
}
public static int stop(){
iGlobalCounter--;
Log.d("PROCESS", "DOWN: "+iGlobalCounter);
if (iGlobalCounter<=0){
//Destroy the object "iThing"
}
}
}
主GUI(在p2中)在onCreate / onDestroy上启动和停止变量(对于我的应用程序中的所有视图)
public class MyMainClass extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Connections.start();
}
@Override
public void onDestroy(){
super.onDestroy();
Connections.stop();
}
}
最后在p1中我有服务,它也需要变量,所以,它做同样的
public class MyMainService extends Service{
@Override
public void onCreate() {
super.onCreate();
Connections.start();
}
@Override
public void onDestroy(){
super.onDestroy();
Connections.stop();
}
}
问题是如果我只使用 p2 (GUI),一切顺利,但是当我执行服务(在 p1 中)时,计数器不会从最后一个状态增加,而是从 0 增加,导致在离开服务时销毁对象,不是应用程序。
如果进行此导航,我会得到以下计数器: 我的主类 (1) -->其他类(2) -->另一个类 (3) --> MyMainService (1)
我的问题是是否有办法拥有多进程全局变量?似乎每个进程都有自己的静态变量,而不是“真正的静态”。解决方案可能是使用 SharedPreferences 来保存状态,但这不是很好的解决方案,因为在离开应用程序时不会保存状态。
谢谢, 帕奥
I have a real problem using my app that involve 2 processes. One process its executing a Service (p1) and the other the GUI (p2).
I have a class in p2 that implements the use of an object (iThing) that is custom memory managed (and its static). It has to be like this bacause of Android OS implementation of destroying the views whenever he wants.
public class Connections{
public static int iGlobalCounter=0;
public static Object iThing;
public static void start(){
iGlobalCounter++;
Log.d("PROCESS", "UP: "+iGlobalCounter);
if (iGlobalCounter<=1){
//Create the object "iThing"
}
}
public static int stop(){
iGlobalCounter--;
Log.d("PROCESS", "DOWN: "+iGlobalCounter);
if (iGlobalCounter<=0){
//Destroy the object "iThing"
}
}
}
The main GUI (in p2), starts and stops the variable on the onCreate / onDestroy (for all views in my app)
public class MyMainClass extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Connections.start();
}
@Override
public void onDestroy(){
super.onDestroy();
Connections.stop();
}
}
Finally in p1 I have the service, which also needs the variable, so, it does the same
public class MyMainService extends Service{
@Override
public void onCreate() {
super.onCreate();
Connections.start();
}
@Override
public void onDestroy(){
super.onDestroy();
Connections.stop();
}
}
The problem is that if I use only p2 (GUI), it goes all well, but when I execute the service (in p1), the counter doesn't increment from the last state, but from 0, resulting in destroying the object when leaving the service, not the app.
if do this navigation, I get the following counters:
MyMainClass (1) --> OtherClass (2) --> AnotherClass (3) --> MyMainService (1)
My question is if there is a way of having a multi-process global variable? As it seems that every process takes its own static variables and are not "real static". A solution could be using SharedPreferences to save the state, but not really nice solution, as it hasn't to be saved when leaving the app.
Thanks,
PAU
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该扩展
Application
类并将您的globalVariable
放在那里。I think that you should extend
Application
class and put yourglobalVariable
there.您可以将全局数据存储在共享内存中(请参阅 MemoryFile)。
为了同步对文件的访问,我认为最好的方法是使用相同的内存文件实现某种自旋锁。
在这种情况下,我不知道这样做的简单方法。
You can store your global data in shared memory (see MemoryFile).
To synchronize access to the file, I think the best approach is to implement some sort of spinlock using the same memory file.
In and case, I don't know a simply way of doing this.
您可以使用以下选项来在不同进程、
消息队列、
命名管道、
命名管道上的内存映射文件
WCF 或 MSMQ之间共享数据
You have the following options which you can look into for sharing data between different processes,
Message Queue,
Named Pipes,
Memory mapped files
WCF on Named Pipes or MSMQ