Skip to content Skip to sidebar Skip to footer

Separation Of Logic And Gui In Android App. Service Has Knowledge Of App And Shouldn't

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

Solution 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.

Solution 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 = newIntent(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

PendingIntentintentBack= 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

Post a Comment for "Separation Of Logic And Gui In Android App. Service Has Knowledge Of App And Shouldn't"