Andriod Service Bind篇

来源:互联网 发布:软文写作软件 编辑:程序博客网 时间:2024/05/18 00:44

一、步骤:

第一步:在Service中,新增了一个MyBinder类继承自Binder类;
             然后在MyBinder中添加了一个startDownload()方法用于在后台执行下载任务;
第二步:创建一个MyBinder对象;
第三步:在onBind方法,返回MyBinder对象;
第四步:修改activity_main.xml文件,增加绑定的按钮;
第五步:在Activity中
创建了一个ServiceConnection的匿名类,
              在里面重写了onServiceConnected()方法和onServiceDisconnected()方法,
              这两个方法分别会在Activity与Service建立关联和解除关联的时候调用。
              在onServiceConnected()方法中,我们又通过向下转型得到了MyBinder的实例,
             有了这个实例,Activity和Service之间的关系就变得非常紧密了。
             现在我们可以在Activity中根据具体的场景来调用MyBinder中的任何public方法,
             即实现了Activity指挥Service干什么Service就去干什么的功能;
第六步:使用bindService()实现Activity与Service的绑定
             bindService()方法接收三个参数,
             第一个参数就是刚刚构建出的Intent对象,
             第二个参数是前面创建出的ServiceConnection的实例,
             第三个参数是一个标志位,这里传入BIND_AUTO_CREATE表示在Activity和Service建立关联后自动创建Service,
             这会使得MyService中的onCreate()方法得到执行,但onStartCommand()方法不会执行。
第七步:解绑。

             unbindService(conn);  

二、代码

MyService的代码:
public class MyService extends Service {    public static final String TAG = "MyService";     private MyBinder mBinder = new MyBinder();          @Override    public void onCreate() {        super.onCreate();        Log.d(TAG, "onCreate() executed");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG, "onStartCommand() executed");         return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Log.d(TAG, "onDestroy() executed");        super.onDestroy();    }    @Override    public IBinder onBind(Intent arg0) {        return mBinder;    }    class MyBinder extends Binder {                public void startDownload() {              Log.d(TAG, "startDownload() executed");              // 执行具体的下载任务          }       } }
Activity_main.xml代码
  <EditText android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:cursorVisible="false"        android:editable="false"        android:id="@+id/editText"/>         <Button        android:id="@+id/btnStartService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="start service" />    <Button        android:id="@+id/btnStopService"                android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="stop service" />            <Button          android:id="@+id/bind_service"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:text="Bind Service" />            <Button           android:id="@+id/unbind_service"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:text="Unbind Service"          />  
MainActivity.java
public class MainActivity extends Activity {private Button btnstart,btnstop,btnbind,btnunbind;private MyService.MyBinder myBinder;    private ServiceConnection conn = new ServiceConnection() {    @Override    public void onServiceDisconnected(ComponentName name) {    }    @Override    public void onServiceConnected(ComponentName name, IBinder service) {              myBinder = (MyService.MyBinder) service;                myBinder.startDownload();          }    };     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnstart = (Button)findViewById(R.id.btnStartService);        btnstop = (Button)findViewById(R.id.btnStopService);        btnbind = (Button)findViewById(R.id.bind_service);        btnunbind = (Button) findViewById(R.id.unbind_service);        setListener();    }     private void setListener() {        startServiceClickListener();        stopServiceClickListener();        bindServiceClickListener();        unbindServiceClickListener();    }     private void unbindServiceClickListener() {        btnunbind.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            unbindService( conn);            }        });    }     private void bindServiceClickListener() {      btnbind.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {            Intent intent=new Intent(MainActivity.this, MyService.class);            bindService(intent, conn, BIND_AUTO_CREATE);          }        });    }    private void stopServiceClickListener() {        btnstop.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            Intent intent=new Intent(MainActivity.this, MyService.class);            stopService(intent);        }        });    }    private void startServiceClickListener() {        btnstart.setOnClickListener(new OnClickListener() {        @Override        public void onClick(View v) {            Intent intent=new Intent(MainActivity.this, MyService.class);            startService(intent);            }        });      }}


0 0
原创粉丝点击