在长期运行的Android服务中,成员变量有时会变为null

时间:2013-02-21 00:17:17

标签: android

我有一个Android服务,它收集该服务的成员变量中的一些数据,这些数据偶尔(远非总是)变为null。无论这个服务在Android设备阵列上运行多长时间,我自己都无法生成这个,所以我想知道是否有人看到跟随中的任何错误(请注意这是一个简单的示例,只是说明了问题):

public class CollectionService extends Service {
    private final CollectionServiceBinder binder = new CollectionServiceBinder();
    private PowerManager.WakeLock wakeLock;
    private UserData userData; // this is the object that sometimes becomes null

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        wakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AppId");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    public void startCollecting() {
        userData = new UserData();
        Notification notification = new Notification();
        // initializing notification variable

        startForeground(0xABCD, notification);

        // trigger logic that collects relevant data here, just imagine some Runnable that's triggered on a certain interval and adds data to the UserData value object.
    }

    public UserData finishCollecting() {
        try {
            userData.setFinishDate(new Date()); // throws NullPointerException every now and then.       return userData;
        } finally {
            if (wakeLock.isHeld()) {
                wakeLock.release();
            }
            stopForeground(true);
            userData = null;
        }
    }

    public boolean isCollecting() {
        return userData != null;
    }

    public class CollectionServiceBinder extends Binder {
        public CollectionService getService() {
            return CollectionService.this;
        }
    }
}

使用...

在活动中启动服务
Intent i = new Intent(this, CollectionService.class);
getApplicationContext().startService(i);
getApplicationContext().bindService(i, serviceConnection, BIND_AUTO_CREATE); // just so the activity has a handle and can call startCollecting()/finishCollecting()

...并且使用...

在onDestroy()中未绑定
getApplicationContext.unbindService(serviceConnection);

ServiceConnection类如下所示:

public class MyServiceConnection implements ServiceConnection {
    private CollectionService service;

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder binder) {
        service = ((CollectionService.CollectionServiceBinder) binder).getService();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        service = null;
    }

    public CollectionService getCollectionService() {
        return service;
    }
}

并且在这样的OnClickListener中触发startCollecting以防止开始两次:

if (!serviceConnection.getCollectionService().isCollecting()) {
    serviceConnection.getCollectionService().startCollecting();
}    

我相信这应该都可以,但有时userData(在代码示例中注释)将为null。同样,这种情况极少发生(在Google Play上安装了50,000多个有源设备我在一年中收到了50份报告,但我仍然希望每个人都能够享受该应用)。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我没有看到您调用startCollecting的地方与您正在初始化userData的地方相关,但可能发生的事情是您的服务因任何原因而被杀死然后重新启动。你需要做一些事情来保持/重新加载数据,或者至少是懒惰的负载userData,这样他们才会重新开始。

此处没有您的所有代码,但也可能需要注意您只在onCreate中获取了唤醒锁,但是您在finishCollecting中将其释放。如果您可能在同一个startCollecting上再次呼叫finishCollectingstartCollecting,然后Service,那么您最终将会Service无法运行唤醒锁定。