<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:
点击绑定:
点击启动:
点击关闭服务:
无输出。
点击解除绑定:
总结:
当我进行启动服务和绑定服务后,如果只是关闭服务或解绑服务并不会将service杀死。
当我进行启动服务后在绑定,并不会重复创建service。
先启动在绑定的流程:构造方法->oncreate->onstartcommand->onstart->onbind->onserviceconnection
先绑定service在启动service流程:构造方法->oncreate->onbind->onserviceconnection->onstartcommand->onstart
当我重复启动service只会重复调用startcommand和start方法,不会重复调用oncreate
当我进行多次绑定时并不会重复创建多个service
当我绑定service并且退出activity不解绑service时,service会自动被关闭,并且logcat会报错
我可以利用绑定service后启动service然后退出activity解绑service,这样service还不会被kill,再次进入activity我们可以通过关闭service使service关闭。
时间: 2024-10-05 17:35:43