通过回调实现Service中更新UI。

来源:互联网 发布:知微公司 编辑:程序博客网 时间:2024/06/05 07:51

Service中更新Activity的UI有2种方法,用广播实现的文章很多,本文是通过回调实现的。

具体步骤如下:

一、MyService中:

1. 声明接口CallBack 并为其添加抽象方法onDataChange(String data);  目的是为了在Activity中实例化。

        2. 声明一个callBack,并添加set方法; 为了把Activity中实例化的callBack传给他。

3.在线程中 if (callBack != null ) 调用onDataChange( changingData)方法,并传入一直改变的参数;

        4. 声明MyBinder,添加getService方法,返回MyService. this 。


二、Activity中:

1.通过onServiceConnected传入的Binder获得myService对象。

2. myService. setCallBack(new CallBack){...... 通过匿名实例化接口的方式,重写onDataChange() , 

     这个实例化的callBack及重写的方法在Myservice的线程中调用,并传入参数。

        3. 因为线程无法更新UI,要在onDataChange() 通过handler 发送Message,之后用Handler更新UI。


MyService 
代码如下:
public class MyService extends Service {    private static boolean running;    public MyService() {    }    public class MyBinder extends Binder{        public MyService getService(){            return MyService.this;        }    }    public IBinder onBind(Intent intent) {         return new MyBinder();    }    @Override    public void onCreate() {        running=true;        new Thread(new Runnable() {            @Override            public void run() {                int i = 0;                while (running){                    i++;                    if(callBack!=null){                        callBack.onDataChange(i);                    }                    try {                        Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();        super.onCreate();    }    public interface CallBack{        void onDataChange(int data);    }    private CallBack callBack;    public void setCallBack(CallBack callBack) {        this.callBack = callBack;    }    @Override    public void onDestroy() {        running=false;        super.onDestroy();    }}

Activity代码如下:

public class MainActivity extends ActionBarActivity {    TextView tv ;    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.tvShowService);        Button button = (Button) findViewById(R.id.btnBindService);    }    ServiceConnection connection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            final MyService.MyBinder binder= (MyService.MyBinder) service;            MyService myService = binder.getService();            myService.setCallBack(new MyService.CallBack() {                public void onDataChange(int data) {                    Message message = new Message();                    Bundle bundle = new Bundle();                    bundle.putInt("count", data);                    message.setData(bundle);                    handler.sendMessage(message);                }            });        }        @Override        public void onServiceDisconnected(ComponentName name) {        }    };    public void btnBindService(View view) {        bindService(new Intent(this,MyService.class),connection,BIND_AUTO_CREATE);    }    public void btnUnBindService(View view) {        unbindService(connection);    }    Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            Bundle bundle = msg.getData();            Integer i = bundle.getInt("count");            tv.setText(String.valueOf(i));        }    };}



0 0
原创粉丝点击