Java tutorial
/* This file is part of ZAX. ZAX is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. ZAX is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with ZAX. If not, see <http://www.gnu.org/licenses/>. */ package com.inovex.zabbixmobile.activities.fragments; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v4.app.Fragment; import android.util.Log; import com.inovex.zabbixmobile.data.ZabbixDataService; import com.inovex.zabbixmobile.data.ZabbixDataService.ZabbixDataBinder; /** * Base class for fragments connected to the data service. * * This class takes care of binding the service. * */ public abstract class BaseServiceConnectedFragment extends Fragment implements ServiceConnection { private static final String TAG = BaseServiceConnectedFragment.class.getSimpleName(); protected ZabbixDataService mZabbixDataService; @Override public void onStart() { super.onStart(); Log.d(TAG, "onStart"); // we need to do this after the view was created!! Intent intent = new Intent(getActivity(), ZabbixDataService.class); getActivity().getApplicationContext().bindService(intent, this, Context.BIND_AUTO_CREATE); } @Override public void onStop() { super.onStop(); getActivity().getApplicationContext().unbindService(this); } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "onServiceConnected"); ZabbixDataBinder binder = (ZabbixDataBinder) service; mZabbixDataService = binder.getService(); } @Override public void onServiceDisconnected(ComponentName name) { mZabbixDataService = null; } }