在Android应用程序逻辑和图形用户界面的分离。服务具有应用的知识,不应该 [英] Separation of logic and GUI in Android app. Service has knowledge of app and shouldn't

查看:143
本文介绍了在Android应用程序逻辑和图形用户界面的分离。服务具有应用的知识,不应该的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手相对到Android编程(我主要是做C#在Windows上)。我继承了一些code,它由一个服务和应用程序。我想重用的服务和一些其他应用程序的应用程序。我把在图书馆。但有一个问题,一切顺利。该服务有一些code,它提出的主要活动在屏幕上。它召唤出来的名字这个活动。很显然,我不能有共同code。该服务code看起来是这样的:

 最终诠释NOTIFY_1 = 0x1001;
NotificationManager通知=(NotificationManager)context.getSystemService(
                    Context.NOTIFICATION_SERVICE);最后通知通知=新的通知(
            R.drawable.icon_my_color,股票,System.currentTimeMillis的());notify.number = 1;
notify.flags | = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
 notify.ledARGB = android.graphics.Color.CYAN;
notify.ledOnMS = 500;
notify.ledOffMS = 500;意图toLaunch =新意图(背景下,MyBankingActivity.class);
toLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
的PendingIntent intentBack = PendingIntent.getActivity(上下文,0,toLaunch,PendingIntent.FLAG_CANCEL_CURRENT);notify.setLatestEventInfo(上下文,标题,信息,intentBack);
notifier.notify(NOTIFY_1通知);

这code被通过时,一些有趣的事情发生(银行账户为负或某物)的服务运行。问题是MyBankingActivity.class,也就是上述硬$ C $光盘。我从库中删除MyBankingActivity因为它的特殊应用。但是,我必须有旧的应用程序的工作。我只是希望它分离成库和应用程序,所以我可以在另一个应用程序库resuse可观的逻辑。在其他应用程序,我不希望格外被告知这一
银行事件。即使我这样做了,code是目前一项活动的名称硬codeD。

什么是这里的逻辑分离的正确方法?这似乎是服务应该是出版的事件,有兴趣应该订阅的应用程序。至少这是我熟悉的术语。顺便说一句,现在,我刚才注释掉code的整个街区,一切都在旧的应用程序正常工作,当然除了得到这个通知。

我认识(道歉),我的问题是广泛的。因为它可能是更好的男人授之以渔,而不是给他鱼,请随时推荐引用/文章/书籍,更广泛地涵盖这一主题。我已经想看看我有限的图书馆对我自己的,但无论哪种我的书都太介绍(Android的行动就是其中之一),不然我找错了地方。即使建立图书馆和写作服务的主题似乎很难得的。在我看来,有我弱的几个关键概念:
1.如何做一个服务通信事件有兴趣没有应用的知识应用
2.如何可以根据从服务中得到了一些消息的应用程序被带到前台
3.如何利用现有的应用程序,并闯入库(非GUI或非特定应用的东西)和应用程序。
4.通知如何工作....

提前

非常感谢,
戴夫


解决方案

  

这似乎是服务应该是出版的事件,应用程序,有兴趣应该订阅。


该服务发布的情况下,给用户。


  

什么是这里的逻辑分离的正确方法?


这取决于你如何定义正确的。在这种情况下,我可能会通过的PendingIntent 通知中使用作为额外的意图用于启动该服务。 的PendingIntent 工具 Parcelable ,因此去很好地成意图额外。这样,客户决定,当用户点击通知会发生什么 - 所有的服务需要做的就是展示通知相关时。


  

如何做一个服务通信事件有兴趣没有应用

知识应用

这是不相关的在这里psented您的使用情况$ P $。你是不是从通信服务的事件的应用程序。你正在通信从给用户的服务的事件。用户不是一个应用程序。

这就是说,你可以发送一个广播,或者有客户端传递的东西为意图额外的,将作为一个回调机制(例如,的PendingIntent 使者)。


  

如何能根据从服务中得到了一些消息的应用程序被带到前台


的服务呼叫 startActivity()。需要注意的是谁找你讨厌这样做(例如,弹出在他们的游戏中间的一个活动)可以发泄自己的无奈的方式,你不会AP preciate。

用户

你是presently采取的方法 - 显示通知 - 是正确的在大多数情况下


  

如何利用现有的应用程序,并闯入库(非GUI或非特定应用的东西)


这不能抽象地回答,因为任何操作系统也罢。


  

的通知是如何工作的......


在文档,最入门的Andr​​oid这方面的内容书籍。

I'm a relative novice to Android programming (I mostly do C# on Windows). I inherited some code that consists of a service and app. I'd like to reuse the service and some of the app in another app. I'm putting that in a library. All has gone well except for one problem. The Service has some code that puts the main activity up on the screen. It calls out this activity by name. Clearly, I can't have that in common code. The service code looks something like this:

final int NOTIFY_1 = 0x1001;
NotificationManager notifier = (NotificationManager) context.getSystemService(
                    Context.NOTIFICATION_SERVICE);

final Notification notify = new Notification(
            R.drawable.icon_my_color, ticker, System.currentTimeMillis());

notify.number = 1;
notify.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
 notify.ledARGB = android.graphics.Color.CYAN;
notify.ledOnMS = 500;
notify.ledOffMS = 500;

Intent toLaunch = new Intent(context, MyBankingActivity.class);
toLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intentBack = PendingIntent.getActivity(context, 0, toLaunch, PendingIntent.FLAG_CANCEL_CURRENT);

notify.setLatestEventInfo(context, title, message, intentBack);
notifier.notify(NOTIFY_1, notify);

This code gets run by the service when something interesting happens (bank account goes negative or something). The problem is the "MyBankingActivity.class" that is hardcoded above. I have removed MyBankingActivity from the library because it's application specific. However, I do have to have the old app working. I just want it to be separated into a library and app so I can resuse the considerable logic in the library in another app. In that other app, I don't particulary want to be notified of this "banking event". Even if I did, the code is currently hardcoded with one activity's name.

What is the correct way to separate the logic here? It would seem that the service should be "publishing" the event, and apps that are interested should "subscribe". At least that is the terminology I am familiar with. Incidentally, for now, I have just commented out the entire block of code and everything works fine in the old app, except of course getting this notification.

I realize (and apologize) that my question is broad. As it might be better to "teach a man to fish, rather than give him fish", please feel free to recommend references/articles/books that cover this topic more broadly. I have tried to look at my limited library on my own, but either my books are too introductory (Android in Action is one) or I'm looking in the wrong places. Even the topics of setting up libraries and writing services seem hard to come by. As I see it, there are a few key concepts I'm weak on: 1. How does a service communicate events to apps that are interested without knowledge of an app 2. How can an app be brought to the foreground based on some message received from service 3. How to take an existing app and break into a library (for non-gui or non app specific stuff) and app. 4. How notifications work....

Many thanks in advance, Dave

解决方案

It would seem that the service should be "publishing" the event, and apps that are interested should "subscribe".

The service is publishing the event, to the user.

What is the correct way to separate the logic here?

That depends on how you define "correct". In this case, I'd probably pass the PendingIntent to be used with the Notification as an extra on the Intent you used to start the service. PendingIntent implements Parcelable and therefore goes nicely into an Intent extra. This way the client dictates what happens when the user taps the Notification -- all the Service needs to do is display the Notification when relevant.

How does a service communicate events to apps that are interested without knowledge of an app

That is not relevant for your use case presented here. You are not communicating an event from a service to an app. You are communicating an event from a service to the user. The user is not an app.

That being said, you can send a broadcast, or have the client pass something as an Intent extra that will serve as a callback mechanism (e.g., PendingIntent, Messenger).

How can an app be brought to the foreground based on some message received from service

The service calls startActivity(). Note that users who find you annoying for doing this (e.g., popping up an activity in the middle of their game) may vent their frustration in ways that you will not appreciate.

The approach you are presently taking -- displaying a Notification -- is the correct one in most cases.

How to take an existing app and break into a library (for non-gui or non app specific stuff)

That cannot be answered in the abstract, for any operating system worth mentioning.

How notifications work.....

That is covered in the documentation and most introductory Android books.

这篇关于在Android应用程序逻辑和图形用户界面的分离。服务具有应用的知识,不应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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