我直接贴出代码 读者慢慢体会吧
接口
public interface ServiceInterface { public void show(); }
Service类
import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class Service_01 extends Service { private final String TAG = "Service--->"; private final IBinder binder = new MyBinder(); MyBinder mybinder = new MyBinder(); public class MyBinder extends Binder implements ServiceInterface { /*show只是测试方法,可以不要*/ public void show() { Toast.makeText(Service_01.this, "MyName is Service_01", Toast.LENGTH_LONG).show(); } /*返回service服务,方便activity中得到*/ Service_01 getService() { return Service_01.this; } } /////////////////以上是为了测试绑定功能而做的准备 /* 绑定时执行*/ @Override public IBinder onBind(Intent intent) { Log.d(TAG, "onBind"); return binder; } /*只在创建时执行一次*/ @Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); } /*断开绑定或者stopService时执行*/ @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } /* 当内存不够时执行改方法 */ @Override public void onLowMemory() { super.onLowMemory(); onDestroy();// 注销该service } /* 当从新尝试绑定时执行 */ @Override public void onRebind(Intent intent) { super.onRebind(intent); Log.d(TAG, "onRebind"); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Log.d(TAG, "onStart"); } /* ,每次startService都会执行该方法,而改方法执行后会自动执行onStart()方法 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand-->flags=" + flags + " startId=" + startId); return super.onStartCommand(intent, flags, startId); } /*断开绑定时执行*/ @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "onUnbind"); return super.onUnbind(intent); } }
Activity测试类
import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class TestService extends Activity { /** Called when the activity is first created. */ /*注册按钮*/ private Button start = null; private Button stop = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); start = (Button)findViewById(R.id.button_01); start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*绑定service*/ Intent bindIntent = new Intent(TestService.this, Service_01.class); bindService(bindIntent, sconnection, Context.BIND_AUTO_CREATE); } }); stop = (Button)findViewById(R.id.button_02); stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*断开绑定service*/ unbindService(sconnection); } }); } private Service_01 serviceBinder; /* 注册接口方法*/ ServiceInterface mService = null; /* 绑定service监听*/ ServiceConnection sconnection = new ServiceConnection() { /*当绑定时执行*/ public void onServiceConnected(ComponentName name, IBinder service) { Log.d("activity--->", "已绑定到service"); mService = (ServiceInterface) service; if (mService != null) { mService.show();//测试方法 } Intent intent = new Intent();//这里只是为了下面传intent对象而构建的,没有实际意义 /*绑定后就可以使用Service的相关方法和属性来开始你对Service的操作*/ serviceBinder = ((Service_01.MyBinder) service).getService(); /*比如:你可以掉用Service的onStartCommand()方法*/ serviceBinder.onStartCommand(intent, 0, 0);//0,0是我随意的参数 } /*当断开绑定时执行,但调用unbindService()时不会触发改方法*/ public void onServiceDisconnected(ComponentName name) { Log.d("activity--->", "已断开绑定service"); } }; }
希望对读者有所帮助