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