[英]Android how to broadcast background notification to current active activity?
I'm new to android and I know this question has been asked a lot, but I didn't find a good solution. 我是android的新手,我知道这个问题已经问了很多,但是我没有找到一个好的解决方案。
I have a singleton SocketIO class which keep receiving information at the background. 我有一个单例SocketIO类,该类始终在后台接收信息。 at the same time I have lots of activities doing different stuff.
同时,我有很多活动在做不同的事情。
now what I what to do is when the SocketIO class received a singal, I need to interrupt the current activity and pop up an alert dialog to show the alert Dialog. 现在,我要做的是当SocketIO类收到单个消息时,我需要中断当前活动并弹出一个警报对话框以显示警报对话框。 And when user click on the "yes" button, it goes to a certain activity to do it's work.
并且,当用户单击“是”按钮时,它会进入某个活动以完成其工作。
Now I put the socketIO client in a singleton class and write a receiver in each activities to receiver notification from socketIO class. 现在,我将socketIO客户端放在单例类中,并在每个活动中编写一个接收者,以接收来自socketIO类的接收者通知。 But the problem is I have lots of activities doing different stuff, and the receiver in each activities are actually the same.
但是问题是我有很多活动在做不同的事情,并且每个活动中的接收者实际上是相同的。 it's kind of a waste of resources and not efficient.
这有点浪费资源,却没有效率。 Is there any more efficient way to call current running foreground activity from back end non-activity class?
有没有更有效的方法从后端非活动类中调用当前正在运行的前台活动?
the Singleton SocketIO class and the receiver in each Activities are like in this link Singleton SocketIO类和每个Activity中的接收器都与此链接相似
Use SharedPreferences in your singletons. 在单例中使用SharedPreferences。
This is how you use it 这就是你的用法
SharedPreferences sharedpreferences;
Editor editor = sharedpreferences.edit();
editor.putString("NAMETOCALL", "valuetostoreforlateruse");
editor.commit();
And this is how you access it for notification 这就是您访问通知的方式
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedpreferences.getString("NAMETOCALL", null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.