通过回调实现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
- 通过回调实现Service中更新UI。
- 【Android】关于在Service中通过接口回调更新Activity的UI
- 有关Android中Service实现UI更新
- Service通过Broadcast更新UI
- Service通过Broadcast更新UI
- Android 线程中通过回调方式更新界面UI.
- 在Android中实现service动态更新UI界面
- 在Android中实现service动态更新UI界面
- 有关Android中Service实现UI更新(Binder的运用)
- 有关Android中Service实现UI更新(Binder的运用)
- 在service 中更新UI
- Service通过广播Broadcast更新Activity UI
- Service通过广播Broadcast更新Activity UI
- 【Android】Service通过广播更新UI
- 【Android】Service通过广播更新UI
- Service通过广播Broadcast更新Activity UI
- Service通过广播Broadcast更新Activity UI
- 【Android应用实例之四】计时器之通过Service&BroadcastReceiver实现UI动态更新
- Linux中的文件描述符与打开文件之间的关系 :http://blog.csdn.net/cywosp/article/details/38965239
- jsoup初识
- js的alert()使用注意
- 快速Android开发系列通信篇之EventBus
- iOS NSString的常用用法
- 通过回调实现Service中更新UI。
- Chromium中添加pak资源
- error C3861: “GdiplusStartup”: 找不到标识符
- AT89C51特殊功能寄存器速查表
- Python特殊语法:filter、map、reduce、lambda
- ubuntu14.04下安装ia32-libs的方法
- iOS开发-控件基类:UIView
- 如何减少浏览器repaint和reflow ?
- 统计学习笔记——统计学习三要素