简体   繁体   English

Android如何将后台通知广播到当前活动?

[英]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.

相关问题 在通知android上恢复当前后台活动 - resume current background activity on notification android 恢复通知中的当前后台活动 - Resuming current background activity on notification 仅当APP在android中处于后台时如何激活广播接收器? - How to active Broadcast Receiver only when APP is in background in android? Android N:如何在分屏时单击通知时在当前活动窗口而不是第二个窗口中启动活动? - Android N: How to launch activity in current active window instead of second window when click on notification in split-screen? 如何保持当前活动在后台运行并在android中返回上一个活动 - How to keep current activity run in background and return previous activity in android 通知在Android中启动当前活动时如何通知 - How to be notified when Notification starts the current Activity in Android 如何在收到通知且应用程序处于后台时调用 android 中的另一个活动 - How to call another activity in android when notification received and app is in background Android通知栏打开上次活动活动? - Android notification bar open last active activity? Android:如何在不退出当前活动的情况下使用VLC? (用于背景音频) - Android : How to use VLC without exiting current Activity ? (For background audio) android:通过通知将当前活动置于最前面 - android: bring current activity to front via notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM