Activity和Service交互之bindService(回调更新UI)

Posted 罗夏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activity和Service交互之bindService(回调更新UI)相关的知识,希望对你有一定的参考价值。

一.回调接口

public interface OnProgressListener {
    void onProgress(int progress);
}

 

二.Service代码

public class MyService extends Service {

    private int progress = 0;
    private OnProgressListener onProgressListener;

    class DownLoadBinder extends Binder{
        public MyService getService(){
            return MyService.this;
        }
    }


    // 设置回调接口
    public void setOnProgressListener(OnProgressListener onProgressListener){
        this.onProgressListener = onProgressListener;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new DownLoadBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }



    // 模拟下载
    public  void startDownload(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (progress<100){
                    progress += 10;
                    // 通知调用方
                    if( onProgressListener != null  ){
                        onProgressListener.onProgress(progress);
                    }
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    // 获取progress
    public int getProgress(){
        return progress;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("Service", "onDestroy");
    }
}

 

三.Activity关键代码:

public class MainActivity extends Activity {

    private ProgressBar progressBar;
    private MyService myService;
    private int progress = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = (ProgressBar)findViewById(R.id.progressBar);

        Intent intent = new Intent(this, MyService.class);
        // 标志位BIND_AUTO_CREATE是的服务中onCreate得到执行,onStartCommand不会执行
        bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }



    protected void myClick(View v){
        if( v.getId() == R.id.btn ){
            myService.startDownload();
        }

        if( v.getId() == R.id.btn2 ){
            unbindService(conn);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
    unbindService(conn);
        super.onDestroy();
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            myService = ((MyService.DownLoadBinder)service).getService();

            // 回调接口
            myService.setOnProgressListener(new OnProgressListener() {
                @Override
                public void onProgress(int progress) {
                    progressBar.setProgress(progress);
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}

 

以上是关于Activity和Service交互之bindService(回调更新UI)的主要内容,如果未能解决你的问题,请参考以下文章

Android之Service

进程划分

Android—Service与Activity的交互

Android安卓进阶技巧之——Android Service 服务

Android Service与Activity的交互

Activity与Service进行数据交互