在Service之间传递数据
来源:互联网 发布:手机屏坏怎样导出数据 编辑:程序博客网 时间:2024/05/02 07:14
在Service之间传递数据
方法一(通过Intent传递数据)
通过Intent对象传递数据,我们知道,Intent总是作为一个桥梁,穿梭于安卓程序的四大组件当中,那么在Service中肯定也能通过Intent传递数据,我们启动Service,使用的方法是Intent intent = new Intent(Activity民名称.this, Service名称.class);然后通过startService(intent)启动服务,这个时候,我们既可以在启动Service之前,通过intent.putExtra(.....)等发放传递数据,其实就和Activity之间传递数据是一样的,然后在Service中的onstartCommand()的第一个参数就是传递过来的intent,巧妙的起到了桥梁的作用;
case R.id.btn1: intent.putExtra("data","中国好帅哥"); startService(intent);
@Overridepublic int onStartCommand(Intent intent, int flags, int startId) { //获得从Activity中传递过来的数据 data = intent.getStringExtra("data"); return super.onStartCommand(intent, flags, startId);}方法二(通过Iblinder)
在绑定Service的时候,我们一般会重写onServiceConnected(Component component , Ibinder binder)这个方法,细心的同学会发现,它的第二个参数是一个Ibinder对象,其实,这个Ibinder对象着正是我们在Service中传递过来的
@Overridepublic IBinder onBind(Intent intent) { //返回Mybinder对象 return new Mybinder();}class Mybinder extends Binder{ //设置数据的内容 public void setData(String data){ SendDataService.this.data = data ; } //返回当前的Service public SendDataService getSendDataService(){ return SendDataService.this ; }}我返回的Ibinder对象是自定义的一个Ibinder类的对象,叫做Mybinder,在Mybinder类中定义一个方法,setData(String data);我们就可以通过外界调用这个方法来给当前的data赋值,当然,你也可以另外的一些方法来完成不同的传递数据的功能
@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { //获得Service中的Mybinder mybinder = (SendDataService.Mybinder) service; mybinder.setData("123456");}
扩展:我们如何来侦听Service内部数据的改变呢,为了讲解方便, 我在Service中创建了一个线程,让它每隔1秒钟就改变一次data数据,此外,还需要定义一个接口,在接口中定义一个onDataChange(string str);在Ibinder定义一个方法,返回当前的Service,那么我们就可以在应用中通过Ibinder获得这个Service,从而获得这个借口,实现onDataChange(string str)方法,来侦听data数据的改变
(返回当前的Service)
//设置数据的内容 public void setData(String data){ SendDataService.this.data = data ; } //返回当前的Service public SendDataService getSendDataService(){ return SendDataService.this ; }}(定义一个借口)
/** * 公开的接口,实现数据同步 */public static interface CallBack { void onDataChange(String data);}/** * callBack */private CallBack callBack = null ;/** * getCallBack * @return */public CallBack getCallBack() { return callBack;}/** * setCallBack * @param callBack */public void setCallBack(CallBack callBack) { this.callBack = callBack;}
(线程)
@Overridepublic void onCreate() { super.onCreate(); System.out.println("onCreate"); //新建一个线程(输出数据) new Thread(){ //定义一个i int i = 0 ; public void run() { super.run(); //修改服务的状态为正在运行 running = true ; while(running) { //休眠一秒钟 try { sleep(1000); i++; } catch (InterruptedException e) { e.printStackTrace(); } //定义一个字符串 String str ; str = i + data; if(callBack!=null){ callBack.onDataChange(str); } System.out.println(str); } } }.start();}
然后我们可以在应用中实现这个接口并且获得数据的内容,但是不能直接赋值给textView哦,因为数据的改变是在另外一条线程中的,要通过一个Holder对象,通过传递信息这样的方式来获取到值然后赋给TextView
@Override public void onServiceConnected(ComponentName name, IBinder service) { //获得Service中的Mybinder mybinder = (SendDataService.Mybinder) service; //新建接口,并且获得数据 mybinder.getSendDataService().setCallBack(new SendDataService.CallBack() { @Override public void onDataChange(String data) { //获得Message对象 Message msg = new Message(); //获得Bundle对象 Bundle bundle = new Bundle(); //给Bundle添加数据 bundle.putString("data", data); //给消息添加Bundle对象数据 msg.setData(bundle); //传递消息 handler.sendMessage(msg); } }); } @Override public void onServiceDisconnected(ComponentName name) { } private android.os.Handler handler = new android.os.Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //获得传递过来的消息,兵器赋值给文本 tv.setText(msg.getData().getString("data")); } };}
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 在Service之间传递数据
- Android如何在Activity和Service之间传递数据
- Android如何在Activity和Service之间传递数据
- 通过Intent在Activity|Service之间传递数据
- Service与Activity之间的数据传递
- 在活动之间传递数据
- 在Activity之间传递数据
- EventBus,一个可以在Activity、Fragment、Service之间传递任意类型数据的事件总线框架
- Activity之间的数据传递-service与activity之间的数据传递
- 用SetWindowLong在对话框之间传递数据
- 在存储过程之间传递数据
- Android 在两个 Activity 之间传递数据
- 使用Bundle在Activity之间传递数据
- 在不同Activity之间传递数据
- 如何在两个ViewController之间传递数据
- 如何在两个app 之间传递数据
- 使用Bundle在Activity之间传递数据
- 在不同Activity之间传递数据
- Win8.1 IE11 浏览器无法打开任何网页的解决办法
- Android 自定义标题栏(title栏)
- Android 添加新的联系人代码
- tair(三)--安装配置
- easyui datagrid json出现 $.rows[0].account 原因分析
- 在Service之间传递数据
- QCustomPlot之Items
- premiere cs4导出时 无法读取源文件。请检查其是否移动或被删除。
- 七牛android用户app端手机检测工具
- android shape的使用
- 花生壳是否开始进军硬件
- 欢迎使用CSDN-markdown编辑器
- 天气数据api根据IP地址查询所在地天气预报
- 写在前面