可选择启动活动并使用来自 Android 服务的通知.仅在某个应用程序存在时启动或通知 [英] Optionally starting activities and using notifications from services in Android. Only launch or notify if a certain app is present

查看:10
本文介绍了可选择启动活动并使用来自 Android 服务的通知.仅在某个应用程序存在时启动或通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自己的应用程序中重用了一项服务(它既是绑定"服务又是启动"服务),因为它执行了许多我感兴趣的有用数据采集.一切正常,但我注意到问题.这段代码抛出了一个异常:

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

异常警告未知活动"和您是否检查过清单".现在,这不是因为我没有将活动放在清单中.事实上,这里真的应该抛出异常,因为我的应用程序没有这个活动!这是为原始应用程序编写的活动.一开始我没有注意到,因为只有在收集到的数据超出某个范围时才会触发此代码.我可以更改服务代码,但服务和原始应用程序必须像以前一样继续工作.只是在我的应用程序中,我对为此特定消息弹出一个活动并不特别感兴趣(这对我来说并不重要).
我确实考虑过这个,我可以在所有东西周围扔一个 try/catch 块.但这似乎有点hacky.如果原始应用程序/服务对中出现其他问题,我想了解一下.我想出了以下想法,我需要有人帮助我指明正确的方向.

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.

  1. 服务是否有办法知道当前正在运行哪些应用程序?
  2. 或者目前处于前台?
  3. 或者当前绑定到服务怎么样?

我对 1-3(一般知识和解决这个问题)感兴趣,但 2 可能是最有用的.毕竟,可能有多个应用程序绑定到该服务,而且我不确定是否要因为此消息而将原始应用程序带到前台而不是我自己的应用程序.最后......忘记我说的一切,然后4. 你会如何解决这个问题?

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?

非常感谢,戴夫

推荐答案

你会如何解决这个问题?

How would you solve this?

你从来没有真正说过这个"是什么.我猜这个"是:

You never really stated what "this" is. I am going to guess that "this" is:

我如何让服务通知我的前台活动(如果有),否则引发Notification(或者什么都不做)?

How do I have a service notify a foreground activity of mine if there is one, and otherwise raise a Notification (or perhaps do nothing at all)?

对于那个这个",使用有序广播.正如我在过去的一篇博文中写的那样,这是通知前台活动或引发 Notification 的秘诀:

For that "this", use an ordered broadcast. As I wrote in a blog post from a ways back, here is the recipe for either notifying a foreground activity or raising a Notification:

  1. 定义事件发生时将使用的操作字符串,您希望转到活动或通知(例如,com.commonsware.java.packages.are.fun.EVENT).

在您的活动中动态注册一个 BroadcastReceiever,为上述操作字符串设置一个 IntentFilter 并具有正优先级(过滤器的默认优先级)是 0).然后,此接收器应让 Activity 执行其需要执行的任何操作,以根据此事件更新 UI.接收者还应该调用 abortBroadcast() 以防止其他人获得它.请务必在onStart()onResume() 中注册接收器,并在相应的onStop()onPause 中取消注册接收器() 方法.

Dynamically register a BroadcastReceiever in your activity, with an IntentFilter set up for the aforementioned action string and with a positive priority (the default priority for a filter is 0). This receiver should then have the activity do whatever it needs to do to update the UI based on this event. The receiver should also call abortBroadcast() to prevent others from getting it. Be sure to register the receiver in onStart() or onResume() and unregister the receiver in the corresponding onStop() or onPause() method.

在您的清单中注册一个 BroadcastReceiver,并为上述操作字符串设置一个 .这个接收者应该发出Notification.

Register in your manifest a BroadcastReceiver, with an <intent-filter> set up for the aforementioned action string. This receiver should raise the Notification.

在您的服务(例如,IntentService)中,当事件发生时,调用 sendOrderedBroadcast().

In your service (e.g., an IntentService), when the event occurs, call sendOrderedBroadcast().

如果您只是想通知您的一项前台活动,但如果它不在前台,则什么也不做,请跳过第 3 步.

If you simply want to notify one of your foreground activities, but do nothing if it is not in the foreground, skip step #3.

这里有一个示例项目来演示这一点.

有没有办法让服务知道当前正在运行哪些应用程序?

Is there a way for the service to know what applications are currently running?

您可以询问 ActivityManager,但坦率地说,这需要一个脆弱的应用程序.

You can ask ActivityManager, but, frankly, that's asking for a fragile app.

或者目前在前台?

不可靠.

或者当前绑定到服务怎么样?

Or how about currently bound to the service?

仅当您自己跟踪时.

这篇关于可选择启动活动并使用来自 Android 服务的通知.仅在某个应用程序存在时启动或通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆