Android学习-Service的绑定

来源:互联网 发布:苏宁互联是什么网络 编辑:程序博客网 时间:2024/05/21 09:38

关于service,我在bindService这里卡了一下,这里总结一下bindService。可以参见这篇文章(http://que2010.iteye.com/blog/1339791)

那么我自己的总结:先来说一下MyService里要怎么写:

MyService类

public class MyService extends Service {private MyBinder mbinder = new MyBinder() ;    public class MyBinder extends Binder{    MyService getMyService(){    Log.e("MyService","getMyService");    return MyService.this;    }    }    @Overridepublic IBinder onBind(Intent arg0) {Log.e("MyService","onBind");// TODO Auto-generated method stubreturn mbinder;}    public void play(){    Log.e("MyService","play");    }@Overridepublic void onCreate() {Log.e("MyService","onCreate");// TODO Auto-generated method stubsuper.onCreate();}@Overridepublic void onDestroy() {Log.e("MyService","onDestroy");// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic void onRebind(Intent intent) {Log.e("MyService","onRebind");// TODO Auto-generated method stubsuper.onRebind(intent);}@Overridepublic void onStart(Intent intent, int startId) {Log.e("MyService","onStart");// TODO Auto-generated method stubsuper.onStart(intent, startId);}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.e("MyService","onStartCommand");// TODO Auto-generated method stubreturn super.onStartCommand(intent, flags, startId);}@Overridepublic boolean onUnbind(Intent intent) {Log.e("MyService","onUnbind");// TODO Auto-generated method stubreturn super.onUnbind(intent);}}
BindServiceTestActivity类,主要用于绑定Service

public class BindServiceTestActivity extends Activity {/** Called when the activity is first created. */private Button but1;private Button but2;private MyService myService;private MyBinder binder;private Context mcontext;private boolean isBind  ;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);but1 = (Button) findViewById(R.id.but1);but2 = (Button) findViewById(R.id.but2);Intent intent1 = new Intent(BindServiceTestActivity.this,MyService.class);intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);isBind = bindService(intent1, conn,Context.BIND_AUTO_CREATE);//如果这里用getApplicationContext()那么unbind时也要用这个,将这个上下文提升到                                                                              //全局,而不仅仅是当前Activity的Contextbut1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubLog.e("BindServiceTestActivity", "bind");myService.play();}});but2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(isBind){unbindService(conn);Log.e("BindServiceTestActivity", "unbind");isBind = false ;}}});}@Overrideprotected void onDestroy() {super.onDestroy();}private ServiceConnection conn = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {Log.e("BindServiceTestActivity", "onServiceConnected");// TODO Auto-generated method stubbinder = (MyBinder) service;myService = binder.getMyService();}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubbinder = null ;}};}

当点击bind按钮时:直接就调用了play方法

当点击unbind时:onUnbind-->onDestroy 

这里没有调用onCreate(),是因为之前在调用的时候就指定了AUTO_CREATE,那为啥没得onStart()呢?


另外一种方式:

通过startService(),的话:onCreate()-->onStartCommand-->onStart()    (多次startService,只执行一次onCreate())

stopService()的话: onDestroy()



原创粉丝点击