I have a service I am reusing (it a both a "bound" and "started" service) in my own app because it does a lot of useful data acquisition I'm interested in. Everything was working but I noticed a problem. An exception is thrown in this code:
Intent dialogIntent = new Intent();
dialogIntent.setClassName(service.getBaseContext(), "com.mycompany.receiver.ui.DialogActivity"); // names changed to protect the innocent
dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
dialogIntent.putExtra("message", message);
service.getApplicationContext().startActivity(dialogIntent);
The exception warns about "unknown activity" and "have you checked the manifest". Now, this is not caused by me not putting the activity in the manifest. In fact, there really should be an exception thrown here because my application doesn't have this activity! This was an activity written for the original application. I didn't notice it at first because this code is only hit when the data gathered is outside a certain range.
I can change the service code, but the service and original application have to continue to work as before. It's just that in my application, I'm not particularly interested in popping up an activity for this particular message (it's not that important to me).
I did think about this, and I could just throw a try/catch block around everything. But that seems a little hacky. If something else is going wrong in the original app/service pair, I want to know about it. I came up with the following ideas, which I need a helping hand to point me in the right direction.
- Is there a way for the service to know what applications are currently running?
- Or perhaps currently in the foreground?
- Or how about currently bound to the service?
I'm interested in 1-3 (for general knowledge and for solving this problem) but 2 is probably the most useful. After all, there could be multiple apps bound to the service, and I'm not sure I want to bring the original app to the foreground over my own just because of this message. And finally.... Forget everything I said, and 4. How would you solve this?
I have the same a very similar problem elsewhere in the code:
Intent toLaunch = new Intent();
toLaunch.setClassName(context, "com.mycompany.receiver.ui.BankingActivity");
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 one doesn't throw an exception; it puts up a notification and when I click on it, the original app is launched. This might actually be ok, but again, it might be nice not to have this (or perhaps a compromise - have the notifier notify if the app is at least bound, but not necessarily in foreground). Thoughts?
Many thanks, Dave