It is not necessary to run actions and use notifications from services on Android. Only run or notify if any application is present

I have a service that I reuse (this is both a β€œconnected” and a "running" service) in my own application, because I have a lot of useful data collection that I'm interested in. Everything worked, 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);

An exception warns of "unknown activity" and "have you checked the manifest." Now this is not due to the fact that I did not put activity in the manifest. In fact, there really should be an exception, because my application does not have this activity! It was an activity written for the original app. At first I did not notice this, because this code only gets when the data collected is outside a certain range. I can change the service code, but the service and the original application should continue to work as before. It’s just in my application, I’m not particularly interested in the appearance of activity for this particular message (this is not so important for me).
I thought about it, and I could just throw a try / catch block around everything. But that seems a bit hacked. If something goes wrong in the original pair of applications / services, I want to know about it. I came up with the following ideas in which I need help to point me in the right direction.

  • Is there any way to find out which applications are currently running?
  • Or perhaps now in the foreground?
  • Or what about binding to a service?

I am interested in 1-3 (for general knowledge and for solving this problem), but 2 is probably the most useful. In the end, there may be several applications related to this service, and I'm not sure that I want to bring the original application to the foreground on top of my own just because of this message. And finally .... Forget everything that I said, and 4. How would you solve this?

:

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);

; , , . , , , (, , ), , , ). ?

, Dave

+5
1

?

, "this". , "this":

, , , Notification (, , )?

"this" . , Notification:

  • , , , (, com.commonsware.java.packages.are.fun.EVENT).

  • BroadcastReceiever , IntentFilter ( 0). , . abortBroadcast(), . onStart() onResume() onStop() onPause().

  • a BroadcastReceiver, <intent-filter> . Notification.

  • (, IntentService), , sendOrderedBroadcast().

, , , β„– 3.

, .

, ?

ActivityManager, , , .

, , ?

.

?

.

+7

All Articles