且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

BroadcastReciever生命周期 - 静态变量

更新时间:2023-01-25 17:25:39

从为 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).