问题描述
是否可以将服务中的意图发送给Application类?没有活动?
Is it possible to send an intent from a service to an Application class? Not Activity?
我不知道在特定时间会运行什么活动,因此我在活动类中添加了一个布尔型标志,该标志可检测到该活动并根据接收到的广播发送适当的数据.
I wouldn't know what activity would be running at a particular time, so I am adding a boolean flag in the activity class that detects the activity and sends the appropriate data based on the broadcast received.
推荐答案
如果您的Service
是活动的,那么您的Application
类也是活动的.
If your Service
is active, then your Application
class is active as well.
否则,您将无法使用getApplicationContext()
.
尽管我对永远运行的服务表示怀疑,但如果最后一个当前处于活动状态,则有一种非常干净的方法可以使Service
与特定的Activity
通信.
Although I'm skeptic about a service that runs forever there is a very clean way to make the Service
communicate with a certain Activity
, should the last one be currently active.
这种干净的方法称为 LocalBroadcastManager .
Such clean way is called LocalBroadcastManager.
用于接收数据的Activity
应该在onResume()
中注册一个BroadcastReceiver
,然后在onPause()
中取消注册.
The Activity
meant to receive the data should register a BroadcastReceiver
in onResume()
and unregister it in onPause()
.
您在活动的onCreate()
this.localBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Do what you have to do here if you receive data from the Service.
}
}
您创建了一个过滤器,因此您的活动仅侦听某种类型的信号.
You create a Filter so your Activity only listens to a certain type of signals.
private IntentFilter notifIntentFilter new IntentFilter("com.you.yourapp.MY_SIGNAL");
在onResume()
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(this.localBroadcastReceiver, notifIntentFilter);
在onPause()
LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(this.localBroadcastReceiver);
现在,每当您要将数据发送到活动"时,您的服务都可以致电:
Now whenever you want to send data to your Activity, your Service can call:
final Intent intent = new Intent();
intent.setAction("com.you.yourapp.MY_SIGNAL");
// put your data in intent
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
如果您的Activity
是唤醒,它将响应该信号.否则,如果它在后台或未被实例化,则不会.
If your Activity
is awake, it will respond to the signal. Otherwise, if it's in the background, or it is not instantiated it won't.
您可以将此模式应用于任意数量的活动.
You can apply this pattern to as many Activities as you wish.
仍然,我从未在类中使用过此功能.但是您可以尝试在此处注册您的接收器.这可能行得通,因为如果Application
类被破坏了,BroadcastReceiver
也被破坏了,因此也有可能未注册.
Still, I have never used this inside the Application
class. But you can try to register your receiver there. It might work, since if the Application
class is destroyed, the BroadcastReceiver
is destroyed too and thus probably unregistered as well.
重点是,如果您的应用程序被销毁,您的服务也会被杀死.除非您在另一个过程中启动它.但是,它将拥有它自己的Application
实例;这可能是一件复杂的事情,您现在可能不想详细了解...
The point is, if your Application gets destroyed, your Service will be killed as well. Unless you launched it in another process. But then it will have it's own instance of Application
; and this is a complex thing you probably do not want to get into details now...
重要:由于Application
类未绑定到任何UI组件,因此您可以在服务内部直接执行所需的任何操作.如果您需要操作UI,那么上面描述的模式将很适合您.
Important: since the Application
class is not tied to any UI component, you can do whatever you need directly inside your service. If you need to manipulate the UI, then the pattern described above will work for you.
请阅读有关新的 Android的背景限制.
是的,如果您需要Service调用在Application
类中声明的函数,则可以执行
Oh yeah right, if you need your Service to call a function declared in your Application
class, you can just do
((MyApplication) getApplication()).myFunctionToHandleData(Intent intent);
虽然我不太了解您的问题,但是上述两种方法都可以为您服务.
I didn't really understand your question though, but either of the methods described above should work for you.
这篇关于将广播意图从服务发送到应用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!