Service基础之bind方式启动Service

来源:互联网 发布:dns 一个域名多个ip 编辑:程序博客网 时间:2024/05/18 00:26

Service基础之bind方式启动Service

  • bind方式启动
  • -

bind方式启动

生命周期方法:onCreate –> onBind –> onUnbind –>onDestroy

特点:以绑定的方式去启动一个服务.这种方式启动的服务,启动该服务的组件在退出运行之前一定要跟服务进行解绑。并且,这种方式,可以得到一个控制服务的对象IBinder,通过这个对象,就可以直接跟服务进行数据通信。

综合以上两种服务的启动方式,来实现即得到控制服务的对象,又可以在组件推出之后服务继续保持运行状态。也就先用startService方式去启动服务,再去bindService。

bind方式启动的demo

AndroidManifest代码

 <service android:name=".service.MyService" > </service>

布局文件代码

<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_bindService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="绑定服务" />    <Button        android:id="@+id/btn_unBindService"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="解除绑定" />    <Button        android:id="@+id/btn_startDownload"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="开始下载" />    <Button        android:id="@+id/btn_stopDownload"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="停止下载" /></LinearLayout>

接口代码

package com.example.day5_android_2.interfaced;/** * 用来控制下载的接口 *  * @author Administrator *  */public interface IDownLoad {    /**     * 开始下载     */    void startDownLoad();    /**     * 停止下载     */    void stopDownLoad();}

Service代码

package com.example.day5_android_2.service;import java.util.Timer;import java.util.TimerTask;import com.example.day5_android_2.interfaced.IDownLoad;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.util.Log;public class MyService extends Service {    private Timer timer;    /**     * 这个方法在其他组件绑定到本服务的时候被调用,而return的MyBinder对象是给绑定到本服务的那个组件,用来控制本服务     */    @Override    public IBinder onBind(Intent intent) {        Log.i("LIST", "onBind方法被调用了");        return new MyBinder();    }    /**     * 在服务第一次启动的时候被调用     */    @Override    public void onCreate() {        Log.i("LIST", "onCreate方法被调用了");        super.onCreate();    }    /**     * 在服务退出的时候被调用     */    @Override    public void onDestroy() {        Log.i("LIST", "onDestroy方法被调用了");        super.onDestroy();    }    /**     * 在服务解除绑定的时候被调用,在此方法中,返回值要设置成为true,onRebind方法才会被调用     */    @Override    public boolean onUnbind(Intent intent) {        Log.i("LIST", "onUnbind方法被调用了");        return true;    }    /**     * 在服务重新连接的时候被调用了     */    @Override    public void onRebind(Intent intent) {        Log.i("LIST", "onRebind方法被调用了");        super.onRebind(intent);    }    /**     * 创建内部类MyBinder继承Binder类和实现IDownLoad,通过onBind方法中向Activity返回MyBinder对象     * 在MyBinder类中实现接口的IDownLoad的方法,在方法中调用服务MyService中的方法,然后在Acitivity中就可以     * 通过返回的Binder强制转换成接口实现(因为内部类是私有的),然后通过调用接口的方法,进而调用服务中的方法,从     * 而实现Activity和服务之间的联系     *      *      * @author Administrator     *      */    private class MyBinder extends Binder implements IDownLoad {        @Override        public void startDownLoad() {            start();        }        @Override        public void stopDownLoad() {            stop();        }    }    /**     * 服务中实现的开始下载方法,内部类中的startDownLoad方法中调用,然后在Activity中     * 调用startDownLoad方法,从而调用此方法     */    public void start() {        timer = new Timer();        timer.schedule(new TimerTask() {            int i;            @Override            public void run() {                i = i + 10;                Log.i("LIST", "当前已经下载:" + i + "%");            }        }, 0, 1000);    }    /**     * 服务中实现的暂停下载方法,内部类中的stopDownLoad方法中调用,然后在Activity中 调用stopDownLoad方法,从而调用此方法     */    public void stop() {        timer.cancel();        Log.i("LIST", "停止下载");    }}

Activity代码

package com.example.day5_android_2;import com.example.day5_android_2.interfaced.IDownLoad;import com.example.day5_android_2.service.MyService;import android.support.v7.app.ActionBarActivity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends ActionBarActivity implements OnClickListener,        ServiceConnection {    private IDownLoad iDownLoad;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.btn_bindService).setOnClickListener(this);        findViewById(R.id.btn_unBindService).setOnClickListener(this);        findViewById(R.id.btn_startDownload).setOnClickListener(this);        findViewById(R.id.btn_stopDownload).setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.btn_bindService:            Intent intent = new Intent(this, MyService.class);            bindService(intent, this, BIND_AUTO_CREATE);            break;        case R.id.btn_unBindService:            // 解除绑定            unbindService(this);            break;        case R.id.btn_startDownload:            // 开始下载            iDownLoad.startDownLoad();            break;        case R.id.btn_stopDownload:            // 停止下载            iDownLoad.stopDownLoad();            break;        default:            break;        }    }    /**     * 这个方法在服务被绑定之后调用,返回的IBinder对象可以用来控制服务中的方法,需要把其强转成为接口实现     * ComponentName是被绑定的那个Service组件名     */    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        //把从Service中传过来的service强制转换成接口实现        //用该实现去调用接口中的方法        iDownLoad = (IDownLoad) service;        Log.i("LIST", "onServiceConnected被调用了");    }    /**     * 这个方法是当服务被动解除绑定的时候调用     */    @Override    public void onServiceDisconnected(ComponentName name) {        Log.i("LIST", "onServiceDisconnected被调用了");    }}

效果图
这里写图片描述
这里写图片描述

0 0