首先介绍一下Service的两种启动方式,
第一种是通过是startService方法启动服务,接受一个Inten的参数,首先调用service的onCreage犯法,再调用onStartCommond方法,与Activity类似
第二种是通过 bindService方法启动服务,如果服务未启动,首先会执行onCreate方法,否则只执行onStartCommond方法,在客户端调用bindService方法,服务类的onBind方法会被执行,此时该方法必须返回一个IBinder类,否则客户端的onServiceConnection因为没有参数而不会执行,当客户端的onServiceConnection执行了以后,服务类才与客户端类进行了成功的绑定,通过该binder类,客户端获得服务器的相关执行结果
启动的服务业可以与其进行绑定,绑定就是客户端与服务类共存亡。
package com.example.huanghanqing.testservice; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener,ServiceConnection { Button btnStartService,btnStopService,btnBindService,btnUnbindService,btnGetCurrentNum; Intent i; private int num; MyService myService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnStartService = (Button)findViewById(R.id.btnStartService); btnStopService = (Button)findViewById(R.id.btnStopService); btnBindService = (Button)findViewById(R.id.btnBindService); btnUnbindService = (Button)findViewById(R.id.btnUnbindService); btnGetCurrentNum = (Button)findViewById(R.id.getCurrentNum); i = new Intent(this,MyService.class); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBindService.setOnClickListener(this); btnUnbindService.setOnClickListener(this); btnGetCurrentNum.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnStartService: startService(i);//start service, and the service will not stop until onDesory is called break; case R.id.btnStopService: stopService(i);//service will stoped break; case R.id.btnBindService: bindService(i,this, Context.BIND_AUTO_CREATE); break; case R.id.btnUnbindService: unbindService(this); break; case R.id.getCurrentNum: System.out.println("当前服务的数字是"+myService.getCurrentNum()); } } @Override public void onServiceConnected(ComponentName name, IBinder binder) { System.out.println("onServiceConnected"); myService = ((MyService.MyBinder)binder).getService(); } @Override public void onServiceDisconnected(ComponentName name) { System.out.println("onServiceDisConnection");//this method will called when the APP crashed } }
package com.example.huanghanqing.testservice; import android.app.Service; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.support.annotation.Nullable; import java.util.Timer; import java.util.TimerTask; /** * Created by huanghanqing on 2016/2/22. */ public class MyService extends Service { private Timer timer = null; private TimerTask timerTask = null; private int i = 0; @Nullable @Override public IBinder onBind(Intent intent) { System.out.println("onBind"); return myBinder; } MyBinder myBinder = new MyBinder(); public class MyBinder extends Binder{ public MyService getService(){ return MyService.this; } } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onCreate() { super.onCreate(); System.out.println("onCreate"); startTimer(); } @Override public void onDestroy() { super.onDestroy(); System.out.println("onDestory"); stopTimer(); } public void startTimer(){ if (timer == null) { timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { i++; System.out.println(i); } }; timer.schedule(timerTask,1000,1000); } } public void stopTimer(){ timer.cancel();; timerTask.cancel(); } public int getCurrentNum(){ return i; } }