绑定/取消绑定服务示例(android)

您能给我一个带有后台服务的应用程序的简单示例,该应用程序使用bind / unbind方法启动和停止它吗?我一直在搜索它半小时,但是这些示例使用startService / stopService方法,或者对我来说很难。谢谢。



慕婉清6462132
浏览 542回答 3
3回答

慕标琳琳

您可以尝试使用以下代码: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() {&nbsp; &nbsp; public void onServiceConnected(ComponentName className, IBinder binder) {&nbsp; &nbsp; &nbsp; &nbsp; myServiceBinder = ((MyService.MyBinder) binder).getService();&nbsp; &nbsp; &nbsp; &nbsp; Log.d("ServiceConnection","connected");&nbsp; &nbsp; &nbsp; &nbsp; showServiceData();&nbsp; &nbsp; }&nbsp; &nbsp; public void onServiceDisconnected(ComponentName className) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("ServiceConnection","disconnected");&nbsp; &nbsp; &nbsp; &nbsp; myService = null;&nbsp; &nbsp; }};public Handler myHandler = new Handler() {&nbsp; &nbsp; public void handleMessage(Message message) {&nbsp; &nbsp; &nbsp; &nbsp; Bundle data = message.getData();&nbsp; &nbsp; }};public void doBindService() {&nbsp; &nbsp; Intent intent = null;&nbsp; &nbsp; intent = new Intent(this, BTService.class);&nbsp; &nbsp; // Create a new Messenger for the communication back&nbsp; &nbsp; // From the Service to the Activity&nbsp; &nbsp; Messenger messenger = new Messenger(myHandler);&nbsp; &nbsp; intent.putExtra("MESSENGER", messenger);&nbsp; &nbsp; bindService(intent, myConnection, Context.BIND_AUTO_CREATE);}您可以通过在Activity类上ovverriding onResume()和onPause()绑定到服务。@Overrideprotected void onResume() {&nbsp; &nbsp; Log.d("activity", "onResume");&nbsp; &nbsp; if (myService == null) {&nbsp; &nbsp; &nbsp; &nbsp; doBindService();&nbsp; &nbsp; }&nbsp; &nbsp; super.onResume();}@Overrideprotected void onPause() {&nbsp; &nbsp; //FIXME put back&nbsp; &nbsp; Log.d("activity", "onPause");&nbsp; &nbsp; if (myService != null) {&nbsp; &nbsp; &nbsp; &nbsp; unbindService(myConnection);&nbsp; &nbsp; &nbsp; &nbsp; myService = null;&nbsp; &nbsp; }&nbsp; &nbsp; super.onPause();}请注意,绑定到服务时,仅onCreate()在服务类中调用该方法。在您的Service类中,您需要定义myBinder方法:private final IBinder mBinder = new MyBinder();private Messenger outMessenger;@Overridepublic IBinder onBind(Intent arg0) {&nbsp; &nbsp; Bundle extras = arg0.getExtras();&nbsp; &nbsp; Log.d("service","onBind");&nbsp; &nbsp; // Get messager from the Activity&nbsp; &nbsp; if (extras != null) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("service","onBind with extra");&nbsp; &nbsp; &nbsp; &nbsp; outMessenger = (Messenger) extras.get("MESSENGER");&nbsp; &nbsp; }&nbsp; &nbsp; return mBinder;}public class MyBinder extends Binder {&nbsp; &nbsp; MyService getService() {&nbsp; &nbsp; &nbsp; &nbsp; return MyService.this;&nbsp; &nbsp; }}定义了这些方法之后,您可以在“活动”中找到服务的方法:private void showServiceData() {&nbsp;&nbsp;&nbsp; &nbsp; myServiceBinder.myMethod();}最后,当某些事件发生时,例如_BOOT_COMPLETED_,您可以启动服务public class MyReciever&nbsp; extends BroadcastReceiver {&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; String action = intent.getAction();&nbsp; &nbsp; &nbsp; &nbsp; if (action.equals("android.intent.action.BOOT_COMPLETED")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent service = new Intent(context, myService.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.startService(service);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}需要注意的是启动服务的时候onCreate()和onStartCommand()被调用服务类,当另一个事件发生时由您可以停止你的服务stopService() 请注意,事件侦听器应该是在你的Android清单文件registerd:<receiver android:name="MyReciever" android:enabled="true" android:exported="true">&nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.BOOT_COMPLETED" />&nbsp; &nbsp; &nbsp; &nbsp; </intent-filter></receiver>

四季花海

首先,我们需要了解两件事,客户它向特定服务器发出请求bindService(new Intent("com.android.vending.billing.InAppBillingService.BIND"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mServiceConn, Context.BIND_AUTO_CREATE);这mServiceConn是ServiceConnection类(内置)的实例,它实际上是我们需要用两种方法(用于连接网络的第一个和未连接网络的第二个)实现的接口,以监视网络连接状态。服务器它处理客户端的请求,并制作自己的副本,该副本仅对发送请求的客户端是私有的,并且此服务器在不同线程上运行。现在在客户端,如何访问服务器的所有方法?服务器发送带有IBinder对象的响应。因此,IBinderobject是我们的处理程序,该处理程序Service使用(。)运算符访问所有方法。。MyService myService;public ServiceConnection myConnection = new ServiceConnection() {&nbsp; &nbsp; public void onServiceConnected(ComponentName className, IBinder binder) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("ServiceConnection","connected");&nbsp; &nbsp; &nbsp; &nbsp; myService = binder;&nbsp; &nbsp; }&nbsp; &nbsp; //binder comes from server to communicate with method's of&nbsp;&nbsp; &nbsp; public void onServiceDisconnected(ComponentName className) {&nbsp; &nbsp; &nbsp; &nbsp; Log.d("ServiceConnection","disconnected");&nbsp; &nbsp; &nbsp; &nbsp; myService = null;&nbsp; &nbsp; }}现在如何调用服务中的方法myservice.serviceMethod();这myService是对象,serviceMethod是服务中的方法。并以此方式在客户端和服务器之间建立通信。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android