我在服务文件 (backgroundService.java) 中声明了一个静态 Arraylist变量。收到推送消息(使用 GCM)后,我正在更新静态变量。我正在使用 4.0.4 设备(三星 S3).. 静态变量变为空,一旦我的应用程序进入后台,然后如果我长按 memu 按钮并滑动我的应用程序(一旦我们滑动应用程序完成但不强制停止),服务暂停了一段时间,然后再次启动,即服务的 onDestroy 方法没有被调用,而是在该暂停之后 OnCreate 方法。在该暂停之后,静态 ArrayList 变为空。如何解决?我不希望我的静态 Arraylist 失去它的价值..
此问题仅出现在 Android 4.0+ 设备中。
public class BackgroundService extends Service
{ 私人计时器计时器 = 新计时器();
Context mContext;
String LOG_TAG=BackgroundService.class.getSimpleName();
ApplicationInfo ai;
private static boolean isRunning = false;
public static ArrayList<String> mDMPush=new ArrayList<String>();
@Override
public void onCreate()
{
super.onCreate();
Log.e(LOG_TAG,"on create ");
isRunning = true;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e(LOG_TAG,"mDMPush"+mDMPush);
Log.e(LOG_TAG,"mDMPush outside if else "+mDMPush.size()+" "+mDMPush.isEmpty());
startService();
}
public static boolean isRunning()
{
return isRunning;
}
private void startService()
{
int delay = 100;
int period =500;
final PackageManager pm = getPackageManager();
timer.scheduleAtFixedRate(new TimerTask() {
public void run()
{
ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
try {
ai = pm.getApplicationInfo(packageName, 0);
if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
**Iam doing an action inside this timer using that arraylist mDMPush.. I ll add value to the arraylist once i received a push***
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, delay, period);
}