BroadcastReciever生命周期 - 静态变量 [英] BroadcastReciever Life Cycle -- Static Variables

查看:252
本文介绍了BroadcastReciever生命周期 - 静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

推荐答案

从为<一个文档href="http://developer.android.com/reference/android/content/BroadcastReceiver.html#ReceiverLifecycle"相对=nofollow> 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).

这篇关于BroadcastReciever生命周期 - 静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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