应用程序与Service之间通信
2013-06-30 02:02
253 查看
实现与Service通信有两种方法,第一种使用bindService()来启动Service;
第二种就是使用startService()启动Service,但是使用BroadCast广播作为信使,
首先我们写一个BindService类,其中,必须重写IBinder onBind()方法,该方法返回一个Binder对象,(但是要注意,Binder为抽象类,需要定义一个内部类实现Binder对象),该对象将作为参数传给bindService(Intent intetn,ServiceConnection conn,int flags)方法的第二个参数,然后,在MainActivity中调用bindService()方法,这样应用程序与Service就形成了通信。
运行程序
当点击“ 绑定Service”按钮时,在logcat中,我们可以看到“Service is connected”,同时我们可以看到Service的生命周期当点击“ 绑定Service”按钮时,在logcat中,我们可以看到“Service is connected”,同时我们可以看到Service的生命周期
第二种就是使用startService()启动Service,但是使用BroadCast广播作为信使,
public class BindService extends Service { private int count; private boolean quit; // 定义onBinder方法所返回的对象 private MyBinder binder = new MyBinder(); // 通过继承Binder来实现IBinder类 public class MyBinder extends Binder { public int getCount() { // 获取Service的运行状态:count return count; } } // 必须实现的方法 @Override public IBinder onBind(Intent intent) { System.out.println("Service is Binded"); // 返回IBinder对象 return binder; } // Service被创建时回调该方法。 @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created"); // 启动一条线程、动态地修改count状态值 new Thread() { @Override public void run() { while (!quit) { try { Thread.sleep(1000); } catch (InterruptedException e) { } count++; } } }.start(); } // Service被断开连接时回调该方法 @Override public boolean onUnbind(Intent intent) { System.out.println("Service is Unbinded"); return true; } // Service被关闭之前回调。 @Override public void onDestroy() { super.onDestroy(); this.quit = true; System.out.println("Service is Destroyed"); } @Override public void onRebind(Intent intent) { super.onRebind(intent); this.quit = true; System.out.println("Service is ReBinded"); } }
首先我们写一个BindService类,其中,必须重写IBinder onBind()方法,该方法返回一个Binder对象,(但是要注意,Binder为抽象类,需要定义一个内部类实现Binder对象),该对象将作为参数传给bindService(Intent intetn,ServiceConnection conn,int flags)方法的第二个参数,然后,在MainActivity中调用bindService()方法,这样应用程序与Service就形成了通信。
public class MainActivity extends Activity { Button bind , unbind , getServiceStatus; // 保持所启动的Service的IBinder对象 BindService.MyBinder binder; // 定义一个ServiceConnection对象 private ServiceConnection conn = new ServiceConnection() { // 当该Activity与Service连接成功时回调该方法 @Override public void onServiceConnected(ComponentName name , IBinder service) { System.out.println("--Service Connected--"); // 获取Service的onBind方法所返回的MyBinder对象 binder = (BindService.MyBinder) service; } // 当该Activity与Service断开连接时回调该方法 @Override public void onServiceDisconnected(ComponentName name) { System.out.println("--Service Disconnected--"); } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取程序界面中的start、stop、getServiceStatus按钮 bind = (Button) findViewById(R.id.bind); unbind = (Button) findViewById(R.id.unbind); getServiceStatus = (Button) findViewById(R.id.getServiceStatus); //创建启动Service的Intent final Intent intent = new Intent(); //为Intent设置Action属性 intent.setAction("org.crazyit.service.BIND_SERVICE"); bind.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { //绑定指定Serivce bindService(intent , conn , Service.BIND_AUTO_CREATE); } }); unbind.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { //解除绑定Serivce unbindService(conn); } }); getServiceStatus.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { // 获取、并显示Service的count值 Toast.makeText(MainActivity.this , "Serivce的count值为:" + binder.getCount() , 4000) .show(); } }); } }
运行程序
当点击“ 绑定Service”按钮时,在logcat中,我们可以看到“Service is connected”,同时我们可以看到Service的生命周期当点击“ 绑定Service”按钮时,在logcat中,我们可以看到“Service is connected”,同时我们可以看到Service的生命周期
相关文章推荐
- 如何:在 DHTML 代码和客户端应用程序代码之间实现双向通信
- 应用程序之间的通信[2006-11-14]
- Android Service与Activity之间通信的几种方式
- 应用程序之间的通信 openURL用法总结
- Service 与 Activity之间的通信新方式
- Android中ActivityManagerService与应用程序(客户端)通信模型分析
- Activity与Service通信(不同进程之间)
- Android软件开发之应用程序之间的通信介绍(十八)
- activity 与 service 之间的通信
- Activity通过bindService启动Service后Activity和Service之间的通信!
- Activity和Service之间的通信
- Delphi 两个应用程序(进程)之间的通信
- 使用netlink机制在内核与应用程序之间通信
- Android Service与Activity之间通信的几种方式
- Android Service与Activity之间通信的几种方式
- Android Service与Activity之间通信的几种方式(1)-----通过Binder对象
- 驱动程序与应用程序之间的通信
- 【学习】Service与Activity之间通信的几种方式
- Android Framework------之ActivityManagerService与Activity之间的通信
- Android中Activity、Service和线程之间的通信