热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android开发之Service与Activity数据交互(源代码分享)

Service想要与Activity进行数据交互,首先Activity先得绑定Service.boundservice是service的实现,它允许其他应用程序绑定到

   Service想要与Activity进行数据交互,首先Activity先得绑定Service.bound service是service 的实现,它允许其他应用程序绑定到它并与之交互。要提供bound service,我们必须实现onBind()回调方法。这个方法返回一个内部对象定义的编程接口,Activity可以使用与Service进行交互。那么具体该如何实现呢,首先我们还是一样先创建一个MyService继承Service。然后如何设置:呢。(1)在你的Service,创建一个Binder实例,返回当前的Service实例以及一个Activity可以调用公共方法。(2)返回这个实例的Binder在onBind()回调方法。(3)在Activity里创建ServiceConnection,在onServiceConnected()回调方法接受Binder,并得到服务器的实例。讲的比较啰嗦哦,我们还是看代码来细细品味吧。

  Service的代码

package com.example.f22_service02;import java.util.Random;import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;public class HelloService extends Service {private MyBinder binder=new MyBinder();public class MyBinder extends Binder{public HelloService getService(){return HelloService.this; //返回Service实例,使主程序能调用该方法}@Overrideprotected boolean onTransact(int code, Parcel data, Parcel reply,int flags) throws RemoteException {// TODO Auto-generated method stubLog.i("TAG", "------>"+data.readInt());Log.i("TAG", "------>"+data.readString());reply.writeInt(2);reply.writeString("Rose");return super.onTransact(code, data, reply, flags);}}@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn binder; //回掉方法}public int getRandom(){return new Random().nextInt(100);}@Overridepublic void onCreate() {// TODO Auto-generated method stubLog.i("TAG", "------>Create");super.onCreate();}@Overridepublic boolean onUnbind(Intent intent) {// TODO Auto-generated method stubLog.i("TAG", "------>Unbind");return super.onUnbind(intent);}}


Activity的方法

package com.example.f22_service02;import com.example.f22_service02.HelloService.MyBinder;import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;@SuppressLint("Recycle")
public class MainActivity extends Activity implements OnClickListener {private Button button, button2, button3;private MyBinder binder;private HelloService helloService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) this.findViewById(R.id.button1);button2 = (Button) this.findViewById(R.id.button2);button3 = (Button) this.findViewById(R.id.button3);button.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);}private ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubLog.i("ACTIVITY", "---------〉失去绑定");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubbinder = (MyBinder) service; // 绑定Service,并获得Service实例,接下来就可以调用Service中的方法了helloService = binder.getService();Log.i("ACTIVITY", "---------〉绑定成功");}};@SuppressLint("Recycle")@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button1://绑定ServiceIntent intent=new Intent(MainActivity.this,HelloService.class);bindService(intent, connection, Context.BIND_AUTO_CREATE);//第三个参数是一个标志显示选项绑定break;case R.id.button2://实现Activity与Service的数据交互//Parcel是一个容器,能包含消息(数据和对象引用),可以通过一个IBinde发送Parcel data=Parcel.obtain();data.writeInt(helloService.getRandom());data.writeString("jack");Parcel reply=Parcel.obtain();try {binder.transact(IBinder.LAST_CALL_TRANSACTION, data, reply, 0);} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}Log.i("ACTIVITY", "----Service回传的值---->"+reply.readInt());Log.i("ACTIVITY", "----Service回传的值---->"+reply.readString());break;case R.id.button3://解除绑定unbindService(connection);break;}}}





推荐阅读
author-avatar
a13786812476
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有