1

I have an android recurring service running in background, no Activity running in front of it. Each and every 15min the Service is invoked by an AlamManager (RTC_WAKEUP) using WakeLocks to perform some network action for a cupple of seconds and then it dies. The service uses a static variable storing a PendingIntent object each time it get called. Btw: I know about SharedPreferences or using persistent layout, thats not my question.

My real question is: How could it be that my static variables are still valid? Even I the meantime of 15 min inactivity (real device sleep, does it matter anyway?) my static variables seems valid after all. I thought and read, if no Activity or Service running in the main UI-Thread and/or another worker threads the whole process dies, and the static vaules are erased, too.

If and why android keeps my process still hanging arround, keeping all static variables. An does it correlate with my scheduled AlamManager event. I daresay android keeps my process memory valid!?

4

2 回答 2

2

我的静态变量怎么可能仍然有效?

Android 没有在警报之间终止您的进程。

我思考并阅读,如果在主 UI 线程和/或另一个工作线程中没有运行任何活动或服务,则整个进程都会终止,并且静态值也会被删除。

Android 可以随时终止您的进程。没有正在运行的组件(活动或服务)会增加 Android 在终止其他进程之前终止您的进程的可能性。但是,如果 Android 不需要 RAM,它将不理会您的进程。

但是,由于您不知道每次AlarmManager事件发生时您的进程是否会在附近,因此您需要处理需要初始化静态数据成员的情况。

于 2012-07-03T19:32:05.003 回答
0

由于您使用的是静态变量,因此垃圾收集器不会清除它们,直到拥有它们的类的类加载器也被清除。在“当且仅当垃圾收集器可以回收其定义的类加载器时,可以卸载类或接口”,请参阅http://docs.oracle.com/javase/specs/jls/se5.0/html/execution。 html#12.7了解更多详情

于 2012-07-03T19:31:58.823 回答