-
慕标琳琳
您可以尝试使用以下代码:protected ServiceConnection mServerConn = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder binder) { Log.d(LOG_TAG, "onServiceConnected"); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(LOG_TAG, "onServiceDisconnected"); }}public void start() { // mContext is defined upper in code, I think it is not necessary to explain what is it mContext.bindService(intent, mServerConn, Context.BIND_AUTO_CREATE); mContext.startService(intent);}public void stop() { mContext.stopService(new Intent(mContext, ServiceRemote.class)); mContext.unbindService(mServerConn);}
-
至尊宝的传说
将以下方法添加到您的活动中:private MyService myServiceBinder;public ServiceConnection myConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { myServiceBinder = ((MyService.MyBinder) binder).getService(); Log.d("ServiceConnection","connected"); showServiceData(); } public void onServiceDisconnected(ComponentName className) { Log.d("ServiceConnection","disconnected"); myService = null; }};public Handler myHandler = new Handler() { public void handleMessage(Message message) { Bundle data = message.getData(); }};public void doBindService() { Intent intent = null; intent = new Intent(this, BTService.class); // Create a new Messenger for the communication back // From the Service to the Activity Messenger messenger = new Messenger(myHandler); intent.putExtra("MESSENGER", messenger); bindService(intent, myConnection, Context.BIND_AUTO_CREATE);}您可以通过在Activity类上ovverriding onResume()和onPause()绑定到服务。@Overrideprotected void onResume() { Log.d("activity", "onResume"); if (myService == null) { doBindService(); } super.onResume();}@Overrideprotected void onPause() { //FIXME put back Log.d("activity", "onPause"); if (myService != null) { unbindService(myConnection); myService = null; } super.onPause();}请注意,绑定到服务时,仅onCreate()在服务类中调用该方法。在您的Service类中,您需要定义myBinder方法:private final IBinder mBinder = new MyBinder();private Messenger outMessenger;@Overridepublic IBinder onBind(Intent arg0) { Bundle extras = arg0.getExtras(); Log.d("service","onBind"); // Get messager from the Activity if (extras != null) { Log.d("service","onBind with extra"); outMessenger = (Messenger) extras.get("MESSENGER"); } return mBinder;}public class MyBinder extends Binder { MyService getService() { return MyService.this; }}定义了这些方法之后,您可以在“活动”中找到服务的方法:private void showServiceData() { myServiceBinder.myMethod();}最后,当某些事件发生时,例如_BOOT_COMPLETED_,您可以启动服务public class MyReciever extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals("android.intent.action.BOOT_COMPLETED")) { Intent service = new Intent(context, myService.class); context.startService(service); } }}需要注意的是启动服务的时候onCreate()和onStartCommand()被调用服务类,当另一个事件发生时由您可以停止你的服务stopService() 请注意,事件侦听器应该是在你的Android清单文件registerd:<receiver android:name="MyReciever" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter></receiver>
-
四季花海
首先,我们需要了解两件事,客户它向特定服务器发出请求bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"), mServiceConn, Context.BIND_AUTO_CREATE);这mServiceConn是ServiceConnection类(内置)的实例,它实际上是我们需要用两种方法(用于连接网络的第一个和未连接网络的第二个)实现的接口,以监视网络连接状态。服务器它处理客户端的请求,并制作自己的副本,该副本仅对发送请求的客户端是私有的,并且此服务器在不同线程上运行。现在在客户端,如何访问服务器的所有方法?服务器发送带有IBinder对象的响应。因此,IBinderobject是我们的处理程序,该处理程序Service使用(。)运算符访问所有方法。。MyService myService;public ServiceConnection myConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder binder) { Log.d("ServiceConnection","connected"); myService = binder; } //binder comes from server to communicate with method's of public void onServiceDisconnected(ComponentName className) { Log.d("ServiceConnection","disconnected"); myService = null; }}现在如何调用服务中的方法myservice.serviceMethod();这myService是对象,serviceMethod是服务中的方法。并以此方式在客户端和服务器之间建立通信。