可选开始活动,并使用通知从Android的服务。只有启动或通知,如果某个应用程序是present [英] Optionally starting activities and using notifications from services in Android. Only launch or notify if a certain app is present

查看:173
本文介绍了可选开始活动,并使用通知从Android的服务。只有启动或通知,如果某个应用程序是present的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我重复使用的服务(它既有绑定和开始的服务),在我自己的应用程序,因为它做了很多有用的数据采集,我感兴趣的一切工作,但我注意到一个问题。一个例外是抛出这个code:

 意图dialogIntent =新意图();
    dialogIntent.setClassName(service.getBaseContext(),com.mycompany.receiver.ui.DialogActivity); //名称更改为保护无辜
    dialogIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    dialogIntent.putExtra(消息,消息);
    。service.getApplicationContext()startActivity(dialogIntent);
 

的异常警告关于未知的活动和有你检查清单。现在,这是不是我不把活动中的表现引起的。事实上,真的应该在这里抛出一个异常,因为我的应用程序不会有这个活动!这对原来的应用程序编写的活动。我并没有在第一次看到它,因为这code仅击中时,收集的数据超出一定的范围内。 我可以更改服务code,但服务和原有的应用程序必须继续像以前那样工作。只是,在我的应用程序,我不是在弹出的活动这种特定的消息(这并不是说对我很重要)特别感兴趣。
我没有想到这一点,我可以扔周围的一切在try / catch块。但是,这似乎有点哈克。如果别的东西是怎么了?在原有的应用程序/服务对,我想知道这件事情。我想出了以下的想法,我需要一个帮手点我在正确的方向。

  1. 有没有办法为服务,以了解哪些应用程序正在运行?
  2. 或者,也许目前的前景?
  3. 或者怎么样当前绑定的服务?

我很感兴趣,1-3(一般知识和解决这个问题),但2可能是最有用的。毕竟,有可能是绑定到服务的多个应用程序,我不知道我希望把原有的应用推到前台了我自己的这条消息只是因为。 最后.... 忘记一切,我说了, 4.你将如何解决这个问题?

我有同样的一个非常类似的问题,在code别处:

 意图toLaunch =新意图();
    toLaunch.setClassName(上下文中,com.mycompany.receiver.ui.BankingActivity);

    toLaunch.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intentBack = PendingIntent.getActivity(上下文,0,toLaunch,PendingIntent.FLAG_CANCEL_CURRENT);

    notify.setLatestEventInfo(背景下,产权,信息,intentBack);
    notifier.notify(NOTIFY_1通知);
 

这一个不抛出异常;它提出了一个通知,当我点击它,原来的应用程序启动。这实际上可能是好的,但是,它可能是不错的不要有这个(或可能是一个妥协 - 有通知通知,如果应用程序至少绑定,但不一定在前台)。思考?

非常感谢, 戴夫

解决方案
  

你将如何解决这个问题?

您写了一个可怕的很多的话,从来没有真正说什么这。我要去猜测,这是:

  

我如何有一个服务通知我的一个前台活动,如果有一个,否则养通知(或者什么也不做)?

有关,这,用一个有序的广播。正如我在一篇博客文章,从一个方面回写道,这里是几招或者通知前台活动或抚养通知

  1. 当你想去的活动或通知(例如, com.commonsware.java.packages.are.fun.EVENT <发生的事件定义,您将使用操作字符串/ code>)。

  2. 动态注册 BroadcastReceiever 在你的活动中,有一个的IntentFilter 设置为上述操作字符串并具有正的优先级(缺省优先级的滤波器是0)。然后,该接收器应该具有活动尽一切需要做更新基于此事件的UI。接收机还应该得到它叫 abortBroadcast()来prevent他人。一定要注册在 ONSTART接收器() onResume()和注销接收器在相应的的onStop()的onPause()方法。

  3. 在注册您的清单中包含的BroadcastReceiver ,与&LT;意向滤光器&gt; 设置为上述操作字符串。该接收机应提高通知

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

如果您只是想通知你的前景活动之一,但什么都不做,如果它不是在前台,跳过步骤#3。

下面是一个示例项目演示此。

  

有没有办法为服务,以了解应用程序正在运行的是什么?

您可以要求 ActivityManager ,但是,坦率地说,这是要求一个脆弱的应用程序。

  

或当前的前景吧?

不可靠的。

  

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

只有当你自己跟踪。

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.

  1. Is there a way for the service to know what applications are currently running?
  2. Or perhaps currently in the foreground?
  3. 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

解决方案

How would you solve this?

You wrote an awful lot of words and never really stated what "this" is. I am going to guess that "this" is:

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

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. Define an action string you will use when the event occurs that you want to go to the activity or notification (e.g., com.commonsware.java.packages.are.fun.EVENT).

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

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

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

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

Here is a sample project that demonstrates this.

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

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

Or perhaps currently in the foreground?

Not reliably.

Or how about currently bound to the service?

Only if you track it yourself.

这篇关于可选开始活动,并使用通知从Android的服务。只有启动或通知,如果某个应用程序是present的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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