Separation of logic and GUI in Android app. Service has knowledge of app and shouldn't

守給你的承諾、 提交于 2020-01-07 02:13:07

问题


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


回答1:


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.




回答2:


this has more to do with Intent as it is the line of code that actually starts the other activity.

You have an

Intent toLaunch = new Intent(context, MyBankingActivity.class);

That will tell the system "i want to do something with the activity named: ..." The name of the intent given by the author already tells you what will happen.

The intent then receives a couple of flags

Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP

(long story short) they make sure the activity is brought to front if it still was running somewhere in the background (or to be more precise, if it was on the history stack).

And then comes the PendingIntent

PendingIntent intentBack = PendingIntent.getActivity(
    context, 0, toLaunch, PendingIntent.FLAG_CANCEL_CURRENT);

Im not so familiar with the PendingIntent but i think its not used the right way here.

As for your questions: 1) you can create intents and have apps register a listener for them 2) thats the thing the intent does, it tells the system to bring that activity to the foreground (or other things you can set with flags on the intent) 3) depends on the app/code

4) http://developer.android.com/reference/android/app/NotificationManager.html there you should also find info on all the other topics



来源:https://stackoverflow.com/questions/8659622/separation-of-logic-and-gui-in-android-app-service-has-knowledge-of-app-and-sho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!