BroadcastReciever生命周期 - 静态变量变量、静态、生命周期、BroadcastReciever

由网友(命运在我手中)分享简介:我有一个BroadcastReceiver类。我宣布,其值在一侧的onReceive()方法更新一些静态变量。按我的知识静态变量将保持它的整个的onReceive调用值。是否有当我将失去这些值的可能性(比如我的类将被卸复位静态变量)?这些基本上是一些临时变量,我需要的是适用于多种的onReceive电话。I have...

我有一个BroadcastReceiver类。我宣布,其值在一侧的onReceive()方法更新一些静态变量。按我的知识静态变量将保持它的整个的onReceive调用值。是否有当我将失去这些值的可能性(比如我的类将被卸复位静态变量)?这些基本上是一些临时变量,我需要的是适用于多种的onReceive电话。

I have a BroadcastReceiver class. I have some static variables declared whose value is updated in side the onReceive() method. As per my knowledge static variable will keep it's value across the onReceive calls. Is there any possibility when I will loose those values(Like my class will be unloaded resetting the static variables)? These are basically some temporary variables I need to be available for multiple onReceive calls.

推荐答案

从为 BroadcastReceiver的生命周期 ...

一个BroadcastReceiver对象仅适用于呼叫的onReceive(上下文,意图)的持续时间。一旦这个函数的code返回时,系统会认为要完蛋,不再活跃的对象。

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

这是不会让使用静态变量实用的东西会被迅速清理了系统的感觉。我会尝试使用共享preferences 致电

This isn't going to make the use of static variables practical in the sense that things will be cleaned up quickly by the system. I'd try using SharedPreferences by calling...

context.getShared preferences(MyReceiver,MODE_PRIVATE)

...在接收机的的onReceive(...)办法(更换MyReceiver有一些名称这是有道理的,以您的应用程序)。

...in the receiver's onReceive(...) method (replace "MyReceiver" with some name which makes sense to your app).

阅读全文

相关推荐

最新文章