有没有办法创建一个控制实例来控制我的应用程序的多个活动? [英] Is there a way to create a controlling instance that controls several activities of my app?

查看:22
本文介绍了有没有办法创建一个控制实例来控制我的应用程序的多个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事一个有多个开发人员的项目.我们正在开发一个相当大的应用程序.每个开发者都有几个活动,可以看作是整个主应用的子应用.

I work on a project that has several developers. We work on one rather large app. Every developer has several activities that can be seen as sub-apps of the whole main-app.

我确实意识到,这可能不是最好的设计,但它确实存在,我们必须以某种方式处理它.

I do realize, that this may not be the best design, but it exists and we have to handle it somehow.

现在的主要问题是,我们需要一个 master,它始终处于活动状态并检查 I/O 等,并且可以向每个子应用程序/活动发出状态更改.诸如我们刚刚失去互联网连接"之类的东西.

Now the main issue is, that we need a master, that is always active and checks I/Os etc and that can give out status changes to every sub-app/activity. Something like "we just lost internet connection" etc.

现在,该 master 是一个单例,首先由启动器 Activity 实例化,并且每个 Activity/子应用程序都可以通过根据 Activity 想要接收的更新传递适当的接口来注册.

Right now, that master is a singleton, that is first instantiated by the launcher activity and that every activity/sub-app can register to by passing the appropriate interfaces depending on what updates the activity would like to receive.

这是有效的,但感觉不对,因为单例需要上下文来访问系统资源以确定系统状态,如互联网或 GPS.如果单例被操作系统杀死,那么简单的getInstance"不会有多大用处,因为单例需要以某种方式获取上下文.我已经阅读了关于扩展 Application 类并在那里创建静态成员上下文的内容,但是这个变量必须是 volatile 并且如果整个应用程序处于某种重启后崩溃/终止状态,它可能返回 null.感觉不安全.

This is working, however it doesn’t feel right, because the singleton needs context to access system resources to determine system stati like internet or gps. Should the singleton be killed by OS, than a simple "getInstance" wouldn’t do much good, because the singleton would somehow need to acquire a context. I've read about extending the Application class and creating a static member context there, but this variable had to be volatile AND its possible that it returns null if the entire app is in some restart-after-crash/kill state. It doesn’t feel safe.

此外,还应该有一种可能性,即主机以某种方式打开用户对话框以向用户显示警告等.这些警告在整个应用程序中看起来应该是一样的,开发人员不必担心它何时或为什么突然弹出.现在,这些消息显示为覆盖所有内容的自定义 Toast.当然,它们需要上下文,如果应用程序即将关闭,则可能会出现问题.

In addition, there should also be a possibility that the master somehow opens a user-dialog to display warnings etc to the user. Those warnings should look the same across the entire app and no dev should have to worry about when or why it suddenly pops up. Right now, those messages appear as custom toasts that overlay everything. Of course they require context and if the app is about to close there could be a problem.

总而言之,这就是我们的困境,我正在寻找解决方案.

All in all, that’s the mess we are in and I’m looking for a solution.

那么我如何创建一个安全的主对象或活动(甚至服务?!),它可以将信息传递给不同的活动并发布警告等(甚至可能有能力关闭活动或至少命令它们关闭自己无需注册 can_close 接口).

So how do I create a safe master object or activity (or even service ?!) that can pass info to different activities and post warnings etc (and Maybe even has the ability to close activities or at least order them to close themselves without the need to register a can_close interface).

它应该是安全的,如果在崩溃后 android 仅重新启动处于活动状态的 Activity,它会以某种方式设法也重新启动或至少具有/提供与以前相同的信息.

It should be that safe, that if after a crash android only restarts the activity that was active it somehow manages to also be restarted or at least have/give the same info as before.

每个想法都受欢迎,但不可能彻底修改应用程序(缺乏时间和人力)

Every idea is welcome but total overhauls of the app are just not possible (lack of time and manpower)

推荐答案

这里有一些想法:

  1. 为您需要执行的所有监控创建一个 Service 组件.如果我理解正确,只有在以下情况下才需要此服务一些活动正在运行.所以你可以让它成为 bound服务.让所有活动在启动时bind 到此服务并在它们关闭时解除绑定.服务将在第一个活动绑定.
  2. 为您的所有活动创建一个基类.您可以在此处编写所有常用代码.例如绑定和交换消息的代码与主服务.此类还可以包含实用程序方法用于通知用户等.因此所有活动都将使用相同的方法通知.
  3. 对于用户通知,您可以使用 Status Bar Notification 或创建一个 Fragment 来捕获、聚合并显示通知.你可以有一个通用的菜单项在基本 Activity 类中实现以显示/隐藏此 Fragment.如果您使用状态栏通知,请确保您使用一个聚合您的应用程序的通知.否则,不同的活动可能在状态栏中制造混乱.
  1. Create a Service component for all the monitoring you need to do. If I understand correctly, this service will be required only if some of the activities are running. So you can make it a bound service. Let all activities bind to this service when they start and unbind when they close. The service will be started when the first activity binds.
  2. Create a base class for all your activities. You can write all the common code here. e.g. the code to bind to and exchange messages with the master service. This class can also contain utility methods for notifying user etc. So all activities will use the same method for notification.
  3. For user notifications, you could either use Status Bar Notification or create a Fragment which can capture, aggregate and display the notifications. You can have a common menu item implemented in the base Activity class to show/hide this Fragment. If you use Status Bar Notification, make sure you use one aggregated notification for your app. Otherwise, the different activities might create a clutter in the status bar.

这篇关于有没有办法创建一个控制实例来控制我的应用程序的多个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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