码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-23 18:15:41      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:cti   layout   slist   int   服务   find   command   stat   nullable   

一.回调接口

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)

标签:cti   layout   slist   int   服务   find   command   stat   nullable   

原文地址:http://www.cnblogs.com/itfenqing/p/6753143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!