Android服务 - 填充列表视图onCreate()

时间:2014-06-20 11:02:39

标签: android listview android-service

我已经创建了一项服务(BackupBoundService)。在这里,我使用Retrofit向服务器请求数据的方法。

我在标签视图中有一个包含列表视图的片段。必须使用从服务中的服务器发出的请求返回的数据填充此列表视图。我已经实施了代码,但我获得了NPENullPointerException)。我知道绑定是一个异步事件,我坚信这就是我获得NPE的原因。

这是我的服务类:

private final IBinder binder = new LocalBinder();

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

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

/**  
 * Class passed back to the client activity.
 */
public class LocalBinder extends Binder {
    public BackupBoundService getService() {
        return BackupBoundService.this;
    }
}


/************************************************
 *                                              *
 *              Service Methods                 *
 *                                              *
 ***********************************************/

这是我的Fragment类,我绑定并调用服务类中的公共方法:

private BackupBoundService backupBoundService;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    (...)

    Utils.isMyServiceRunning(BackupBoundService.class); // Here I get the service is running
    backupBoundService.listAllBackupsInTheCloudAsync(); // Here I get the NPE, saying backupBoundService is null
    return view;
}


// Backup Service
private ServiceConnection backupServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        backupBoundService = binder.getService();
        isBound = true;

        Log.d(TAG, "On Service Connected -> Yup!");
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;
    }
};


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    Intent intent = new Intent(ApplicationContextProvider.getContext(), BackupBoundService.class);
    ApplicationContextProvider.getContext().bindService(intent, backupServiceConnection, Context.BIND_AUTO_CREATE);

    Utils.setContext(getActivity());
    Utils.isMyServiceRunning(BackupBoundService.class); // Here I get that service is running
}

那么,我如何避免使用NPE并仍在onCreate()填充列表视图?

检查代码注释以了解NPE发生的位置

这是我的isServiceRunning方法,以防有人想知道:

    public static boolean isMyServiceRunning(Class<?> serviceClass) {
        MainActivity myActivity = (MainActivity) getContext();

        ActivityManager manager = (ActivityManager) myActivity.getSystemService(Context.ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.d(TAG, "YES SERVICE IS RUNNING!");
                return true;
            }
        }
        Log.d(TAG, "NO, SERVICE NOT RUNNING");
        return false;
    }

1 个答案:

答案 0 :(得分:0)

我通过调用onServiceConnected

中的服务方法解决了我的问题
// Backup Service
private ServiceConnection backupServiceConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        LocalBinder binder = (LocalBinder) service;
        backupBoundService = binder.getService();
        isBound = true;

        // Call here my method

        Log.d(TAG, "On Service Connected -> Yup!");
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        isBound = false;
    }
};

这不是最干净和漂亮的方式来做它IMO但它的工作原理。尽管如此,如果有人提供更好,更清洁的解决方案,我会保持这个问题。