绑定活动的service

来源:互联网 发布:linux启动weblogic命令 编辑:程序博客网 时间:2024/05/16 05:47

服务跟活动的区别其实差别不大,主要的区别在于一个有界面一个没有界面。

生命周期:

想对于活动来说,生命周期多了两个方法,onbind()和onunbind()。


活动:

public class Main extends Activity implements OnClickListener {private Intent intent;private Button start;private Button stop;private Button skip;// 保持启动的binder对象MyBinder binder;// 定义服务连接对象ServiceConnection conn = new ServiceConnection() {// 当活动与服务连接断开时,回调该方法@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubToast.makeText(Main.this, "断开连接", 1000).show();}// 当活动与服务连接成功时,回调该方法@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub// 获取onbind()放回的对象binder = (MyBinder) service;}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);initView();}public void initView() {start = (Button) findViewById(R.id.btn_start);stop = (Button) findViewById(R.id.btn_stop);skip = (Button) findViewById(R.id.btn_skip);start.setOnClickListener(this);stop.setOnClickListener(this);skip.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubintent = new Intent();intent.setAction("service_myservice");if (v == start) {// 绑定指定的服务bindService(intent, conn, Service.BIND_AUTO_CREATE);} else if (v == stop) {// 获取serviec内容Log.i("geti:", binder.getI() + "***");// 解除绑定unbindService(conn);} else if (v == skip) {intent = new Intent(this, Second.class);startActivity(intent);this.finish();}}}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btn_start"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="start" />    <Button        android:id="@+id/btn_stop"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="stop" />    <Button        android:id="@+id/btn_skip"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳转" /></LinearLayout>


自定义服务:

public class MyService extends Service {int i = 0;MyBinder myBinder = new MyBinder();// 必须实现的方法@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn myBinder;}// 创建的时候,回调该方法@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();i = 10;Log.i("onCreate", "******");}// 断开连接的时候,回调该方法@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubLog.i("onUnbind", "******");return super.onUnbind(intent);}// 关闭之前,回调该方法@Overridepublic void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.i("onDestroy", "******");}class MyBinder extends Binder {public int getI() {return i;}}}

注意:

        1.绑定的服务,随着访问者的生命消亡而消亡(是指的是访问者被调用了finish而消亡,而不是stop())。

         2.其实更普遍的做法是ibinder返回的是service服务本身,那样能更简单的调用服务的方法。

0 0
原创粉丝点击