zoukankan      html  css  js  c++  java
  • Android Service总结04 之被绑定的服务 -- Bound Service

    Android Service总结04 之被绑定的服务 -- Bound Service

    版本

    版本说明

    发布时间

    发布人

    V1.0

    添加了Service的介绍和示例

    2013-03-17

    Skywang

     

     

     

     

     

     


    1 Bound Service说明

       Bound Service,即被绑定的服务,和Started Service一样,它也是2种常见服务之一。它常被用在执行进程的某个后台操作或进程间通讯(IPC)。

     实现步骤和使用方法

    (01) 创建一个Bound Service类,该类要继承于Service。

    (02) 在Bound Service类中实现以下接口:
      onCreate():可以不用实现,视用户需求而定。当服务被创建时,系统会自动调用该函数。一般在该函数中进行初始化工作,例如:新建线程。
      onDestroy():可以不用实现,视用户需求而定。当服务被销毁时,系统会自动调用该函数。一般在该函数中进行清除工作,例如,终止并回收线程。
      onBind():必须实现!在onBind()中要返回IBinder对象。IBinder对象的作用是让客户端通过IBinder获取该service的对象,从而调用服务提供相关的功能。Anroid传递数据的机制是基于IBinder的,我们不能直接传递service的对象。总之,我们需要记住的是onBind()中需要返回IBinder对象。下面说说IBinder对象怎么获取。
           通常,我们通过在"Bound Service"中创建一个继承于Binder的内部类。在该内部类中添加一个方法,比如getService(),返回“Bound Service”的对象。然后在onBind()中返回该内部类的对象即可。

    (03) 客户端通过bindService()来绑定服务。bindService()中传递的参数包含一个ServiceConnection对象,下面说说怎么获取该对象。
           在客户端中创建一个继承于ServiceConnection的内部类;实现ServiceConnection中的两个抽象函数:onServiceConnected() 和 onServiceDisconnected()。 onServiceConnected()在绑定服务成功时会被系统调用,在onServiceConnected()的输入参数包含“onBinder()中返回的IBinder对象”,根据该对象,我们就能获取到service的对象,之后就可调用service提供的服务。 onServiceDisconnected()在解除绑定时会被系统调用。
           在bindService()中传入该内部类的对象即可。

    (04) 调用Bound Service提供的服务函数接口,以执行相关操作。
           在onServiceConnected()中已经获取到service对象;现在,在我们客户端的任何地方都可以调用到该service提供的服务了。

    (05) 客户端通过unbindService()结束服务。


    2 Service示例

        采用Bound Service来实现“ndroid Service总结03 之被启动的服务 -- Started Service”中的示例,即:编写一个activity,包含2个按钮和1个进度条,2个按钮分别是开始按钮、结束按钮。点击“开始”按钮:进度条开始加载;“开始”变成“重启”按钮;显示“结束”按钮(默认情况,“结束”按钮是隐藏状态)。

    BinderServiceImpl.java的内容:

    package com.skywang.service;
    
    import android.os.Binder;
    import android.os.IBinder;
    import android.app.Service;
    import android.content.Intent;
    import android.util.Log;
    
    import java.lang.Thread;
    /**
     * @desc 服务:每隔200ms将一个数字+2并通过广播发送出去
     * @author skywang
     *
     */
    public class BinderServiceImpl extends Service {
        private static final String TAG = "skywang-->BinderServiceImpl";
        
        // 发送的广播对应的action
        private static final String COUNT_ACTION = "com.skywang.service.binderservice.COUNT_ACTION";
        
        // 线程:用来实现每隔200ms发送广播
        private static CountThread mCountThread = null;
        // 数字的索引
        private static int index = 0;
    
        // 创建IBinder对象
        private final IBinder mBinder = new LocalBinder();
        
        @Override
        public void onCreate() {
            Log.d(TAG, "onCreate");
            super.onCreate();
        }
        
        @Override
        public void onDestroy() {
            Log.d(TAG, "onDestroy");
    
            // 终止服务
            if ( mCountThread != null) {
                mCountThread.interrupt();
                mCountThread = null;
            }
            super.onDestroy();
        }
        
        public void startCount() {
            Log.d(TAG, "startCount");
            
            // 非首次运行服务时,执行下面操作
            // 目的是将index设为0
            if ( mCountThread != null) {
                Log.d(TAG, "mCountThread != null");
                index = 0;
                return ;
            }
            
            Log.d(TAG, "start thread");
            // 首次运行时,创建并启动线程
            mCountThread = new CountThread();
            mCountThread.start();        
        }
        
        public void endCount() {
            // 终止服务
            if ( mCountThread != null) {
                mCountThread.interrupt();
                mCountThread = null;
            }
        }
        
        @Override
        public IBinder onBind(Intent intent) {
            Log.d(TAG, "onBind");
            return mBinder;
        }
        
        public class LocalBinder extends Binder {
            public BinderServiceImpl getService() {
                // Return this instance of LocalService so clients can call public methods
                return BinderServiceImpl.this;
            }
        }
    
        
        private class CountThread extends Thread {
            @Override 
            public void run() {
                index = 0;
                try {
                    while (true) {
                        // 将数字+2,
                        index += 2;                    
                        
                        // 将index通过广播发送出去
                        Intent intent = new Intent(COUNT_ACTION);
                        intent.putExtra("count", index);
                        sendBroadcast(intent);
    //                    Log.d(TAG, "CountThread index:"+index);
                        
                        // 若数字>=100 则退出
                        if (index >= 100) {
                            if ( mCountThread != null) {
                                mCountThread = null;
                            }
                            return ;
                        }
                        
                        // 修改200ms
                        this.sleep(100);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    BinderServiceTest.java的内容:

    package com.skywang.service;
    
    import com.skywang.service.BinderServiceImpl.LocalBinder;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.app.Activity;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.ServiceConnection;
    import android.content.ComponentName;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ProgressBar;
    
    public class BinderServiceTest extends Activity {
        private static final String TAG = "skywang-->BinderServiceTest";
    
        private static final String COUNT_ACTION = "com.skywang.service.binderservice.COUNT_ACTION";
        private CurrentReceiver mReceiver;
        private Button mStart = null;
        private Button mStop = null;
        private Intent mIntent = null;
        private Intent mServiceIntent = null; 
        private ProgressBar mProgressBar = null;
        
        private BinderServiceImpl mService;
        private boolean mBound = false;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.binder_service_test);
    
    
            mStart = (Button) findViewById(R.id.start);
            mStart.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    Log.d(TAG, "click start button");
                    // 显示“结束”按钮
                    mStop.setVisibility(View.VISIBLE);
                    // 将“开始”按钮更名为“重启”按钮
                    mStart.setText(R.string.text_restart);
                    // 启动计数
                    mService.startCount();
                }
            });
            
    
            mStop = (Button) findViewById(R.id.stop);
            mStop.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View view) {
                    Log.d(TAG, "click stop button");
                    // 结束计数
                    mService.endCount();
                }
            });
            mStop.setVisibility(View.INVISIBLE);
            
            mProgressBar = (ProgressBar) findViewById(R.id.pbar_def);
            // 隐藏进度条
            mProgressBar.setVisibility(View.INVISIBLE);
            
    
            // 启动服务,用来更新进度
            mServiceIntent = new Intent("com.skywang.service.BinderService");        
            bindService(mServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
            
            // 动态注册监听COUNT_ACTION广播
            mReceiver = new CurrentReceiver();
            IntentFilter filter = new IntentFilter();
            filter.addAction(COUNT_ACTION);
            this.registerReceiver(mReceiver, filter);
        }
    
        @Override  
        public void onDestroy(){  
            super.onDestroy();  
      
            if(mIntent != null) {
                // 结束服务。
                unbindService(mConnection);
                mServiceIntent = null;
                mBound = false;
            }
            
            if(mReceiver != null)
                this.unregisterReceiver(mReceiver);
        }
        
        private ServiceConnection mConnection = new ServiceConnection() {
    
            /**
             * 绑定服务成功的回调函数
             */
            @Override
            public void onServiceConnected(ComponentName className,
                    IBinder service) {
                Log.d(TAG, "onServiceConnected");
                // 获取IBinder服务对象
                LocalBinder binder = (LocalBinder) service;
                mService = binder.getService();
                mBound = true;
            }
    
            /**
             * 解除绑定的回调函数
             */
            @Override
            public void onServiceDisconnected(ComponentName arg0) {
                Log.d(TAG, "onServiceDisconnected");
                mBound = false;
            }
        };    
        
        /**
         * @desc 更新进度条
         * @param index
         */
        private void updateProgressBar(int index) {
            int max = mProgressBar.getMax();
    
            if (index < max) {
                mProgressBar.setProgress(index);
                mProgressBar.setVisibility(View.VISIBLE);
            } else {
                // 隐藏进度条
                mProgressBar.setVisibility(View.INVISIBLE);
                // 隐藏“结束”按钮
                mStop.setVisibility(View.INVISIBLE);
                // 将“重启”按钮更名为“开始”按钮
                mStart.setText(R.string.text_start);
            }
    //        Log.d(TAG, "progress : "+mProgressBar.getProgress()+" , max : "+max);
        }
        
        /**
         * @desc 广播:监听COUNT_ACTION,获取索引值,并根据索引值来更新进度条
         * @author skywang
         *
         */
        private class CurrentReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action  = intent.getAction();
                if (COUNT_ACTION.equals(action)) {
                    int index = intent.getIntExtra("count", 0);
                    updateProgressBar(index);
                }
            }
        }
    }

    layout文件binder_service_test.xml的内容:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
            <Button
                android:id="@+id/start"  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:text="@string/text_start"
                />
            <Button
                android:id="@+id/stop"  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="@string/text_stop"
                />
            
        </LinearLayout>
    
        <ProgressBar
            android:id="@+id/pbar_def"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:max="100"
            android:progress="0"
            />
    </LinearLayout>

    manifest的内容:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.skywang.service"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="11"
            android:targetSdkVersion="17" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.skywang.service.BinderServiceTest"
                android:screenOrientation="portrait"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            
            <service android:name=".BinderServiceImpl">
                <intent-filter>
                    <action android:name="com.skywang.service.BinderService" />
                </intent-filter>
            </service>
        </application>
    
    </manifest>

    点击下载:源代码

    效果图:

    更多service内容:

    Android Service总结01 目录

    Android Service总结02 service介绍

    Android Service总结03 之被启动的服务 -- Started Service

    Android Service总结04 之被绑定的服务 -- Bound Service

    Android Service总结05 之IntentService

    Android Service总结06 之AIDL


  • 相关阅读:
    最近工作状态异常的原因追寻。
    当“逻辑”与“UE”冲突时
    面对一个“丢失了与用户“签订”的协议的修改”时进行的思考。
    如果公司的需求总是让研发部门有怨言…
    安装sybase12.0,运行时报错异常。
    你看到这份文档,我就想摔鼠标!
    对于研发组长的责任产生了疑惑。
    关于html中空格导致的排版问题
    如何配置你的工作环境。
    今天的笔记:2014年6月3日
  • 原文地址:https://www.cnblogs.com/skywang12345/p/3168522.html
Copyright © 2011-2022 走看看