android开发分享绑定/取消绑定服务示例(android)

你能给我一个简单的应用程序的例子,使用绑定/取消绑定方法启动和停止它的后台服务? 我一直在Google上search半小时,但这些例子使用startService / stopService方法或对我来说是非常困难的。 谢谢。

    你可以尝试使用这个代码:

    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类上的onResume()onPause()来绑定服务。

     @Override protected void onResume() { Log.d("activity", "onResume"); if (myService == null) { doBindService(); } super.onResume(); } @Override protected 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; @Override public 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; } } 

    定义这些方法后,您可以在“活动”中find您的服务方法:

     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清单文件中:

     <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);

    这里mServiceConnServiceConnection类的实例(内置的)它实际上是我们需要实现的两个接口(第一个为networking连接,第二个networking没有连接)监控networking连接状态。

    服务器

    现在在客户端,如何访问服务器的所有方法?

     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是object和serviceMethod是服务的方法。 通过这种方式在客户端和服务器之间build立通信。

    以上就是android开发分享绑定/取消绑定服务示例(android)相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/513964.html

      (0)
      上一篇 2020年11月29日
      下一篇 2020年11月29日

      精彩推荐