<strong><span style="font-size:18px;">自定义Service:</span></strong> /** * Created by liuwang on 15-3-15. */ public class MyService extends Service { private MBinder mBinder = new MBinder(); public MyService() { super(); System.out.println("MyService---"); } @Override public IBinder onBind(Intent intent) { System.out.println("onBind---"); return mBinder; } @Override public void onCreate() { super.onCreate(); System.out.println("onCreate---"); } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); System.out.println("onStart---"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand---"); return super.onStartCommand(intent, flags, startId); } @Override public boolean onUnbind(Intent intent) { System.out.println("onUnbind---"); return super.onUnbind(intent); } @Override public void onDestroy() { super.onDestroy(); System.out.println("onDestroy---"); } @Override public void onRebind(Intent intent) { super.onRebind(intent); System.out.println("onRebind---"); } @Override public void onTaskRemoved(Intent rootIntent) { super.onTaskRemoved(rootIntent); System.out.println("onTaskRemoved---"); } @Override protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { super.dump(fd, writer, args); System.out.println("dump---"); } @Override public void onTrimMemory(int level) { super.onTrimMemory(level); System.out.println("onTrimMemory---"); } @Override public void onLowMemory() { super.onLowMemory(); System.out.println("onLowMemory---"); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); System.out.println("onConfigurationChanged---"); } class MBinder extends Binder { } }Activity:
public class MainActivity extends ActionBarActivity implements View.OnClickListener { private Button start; private Button finish; private Button onbind; private Button unbind; private Intent intent; private ServiceConnection sc; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.start); finish = (Button) findViewById(R.id.finish); onbind = (Button) findViewById(R.id.onbind); unbind = (Button) findViewById(R.id.unbind); start.setOnClickListener(this); finish.setOnClickListener(this); onbind.setOnClickListener(this); unbind.setOnClickListener(this); intent = new Intent(); intent.setClass(this,MyService.class); sc = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { System.out.println("连接成功"); } @Override public void onServiceDisconnected(ComponentName name) { System.out.println("连接失败"); } }; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: startService(intent); break; case R.id.finish: stopService(intent); break; case R.id.onbind: bindService(intent,sc,BIND_AUTO_CREATE); break; case R.id.unbind: unbindService(sc); break; } } }
测试1:
当点击启动Service时的流程:
在点击绑定:
点击解绑:
最后点击关闭服务:
测试2:
点击绑定:
点击启动:
点击关闭服务:
无输出。
点击解除绑定:
原文地址:http://blog.csdn.net/u012706758/article/details/44300407