Start Service启动的Service,Application退出,Service也不会退出;
Bind Service启动的Service,Application退出,Service就会退出。
Start Service和Stop Service仅仅是启动服务,并不能进行通信。通过Bind Service与服务进行绑定,才能与服务通信。
package com.example.servdemo; 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.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener, ServiceConnection { private Button btnStartService, btnStopService, btnBindService, btnUnbindService, btnGetCurrentNum; private Intent serviceIntent; private EchoService echoService = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); serviceIntent = new Intent(this, EchoService.class); 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.btnGetCurrentNum); btnStartService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBindService.setOnClickListener(this); btnUnbindService.setOnClickListener(this); btnGetCurrentNum.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnStartService: startService(serviceIntent); break; case R.id.btnStopService: stopService(serviceIntent); break; case R.id.btnBindService: bindService(serviceIntent, this, Context.BIND_AUTO_CREATE); break; case R.id.btnUnbindService: unbindService(this); echoService = null; break; case R.id.btnGetCurrentNum: if (echoService != null) { System.out.println("The Number is "+echoService.getCurrentNum()); } break; } } @Override public void onServiceConnected(ComponentName name, IBinder binder) { System.out.println("onServiceConnected!"); echoService = ( (EchoService.EchoServiceBinder)binder ).getService(); } @Override public void onServiceDisconnected(ComponentName name) { } }
package com.example.servdemo; import java.util.Timer; import java.util.TimerTask; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; public class EchoService extends Service { @Override public IBinder onBind(Intent intent) { System.out.println("onBind!"); return echoServiceBinder; } private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder(); public class EchoServiceBinder extends Binder{ public EchoService getService() { return EchoService.this; } } public int getCurrentNum(){ return i; } @Override public void onCreate() { // TODO Auto-generated method stub System.out.println("Service Start!"); startTimer(); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("Service Stop!"); stopTimer(); super.onDestroy(); } private int i = 0; 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(){ if(timer != null){ timerTask.cancel(); timer.cancel(); timer = null; timerTask = null; } } private Timer timer = null; private TimerTask timerTask = null; }