安卓之绑定服务

绑定服务
绑定服务提供了一个客户端–服务器接口,允许组件与服务进行交互、发送、请求、获取结果、甚至是利用进程间通信(IPC)跨进程执行这些操作,仅当与另一个应用程序绑定时,绑定服务才会执行,多个组件可以同时绑定服务,但全部取消绑定服务后,该服务即会被销毁。
接下来,我们创建一个简单的关于绑定服务的案例,可以更好的理解绑定服务。
首先我们创建一个安卓项目,在这里我就不介绍如何创建了。
在xml中编写如下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

  <Button
      android:id="@+id/button"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="获取随机数"
      android:onClick="click"
      />
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        />

</LinearLayout>

然后使用安卓自带的创建服务,步骤如下:
在这里插入图片描述
然后创建一个名字叫MyService的服务,在MyService中的代码如下:

public class MyService extends Service {
   private final IBinder binder=new IBinder();
   private final Random random=new Random();

   public class IBinder extends Binder{
       MyService getService(){
           return MyService.this;
       }
   }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
       return binder;
    }
    public int getRandom(){
       return random.nextInt(100);
    }
}

接下来关于上面的进行分析:
自定义一个Binder的子列IBinder,这个类对于绑定时服务的内容实现来说非常重要,IBinder类可以理解为一个服务和前台Activity的通道。可以在自定义的Binder子类中结合实际添加一些功能方法。而后将onBind(Intent intent)函数的返回值改为自定义IBinder 子类的一个实例。在自定义的服务类中,可以自定义一些方法(本文中为 getRandom)便于前台调用。

IBinder是远程对象的接口。在这里是表示客户端与服务交互的程序接口。
Binder相当于一种托管,可以说是中转站,用于配置不同的Service服务。
然后在Java中编写代码如下:

public class MainActivity extends AppCompatActivity {
    boolean bound=false;
    MyService myService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=(Button)findViewById(R.id.bottom);
        final TextView textView=(TextView)findViewById(R.id.textView);

    }
    private ServiceConnection connection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            MyService.IBinder binder=(MyService.IBinder) service;
            myService=binder.getService();//获取Service实例
            bound=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            bound=false;
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent=new Intent(MainActivity.this,MyService.class);
        bindService(intent,connection,BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bound){
            unbindService(connection);
            bound=false;
        }
    }
    public void click(View v){
        int num=myService.getRandom();
        TextView textView=(TextView)findViewById(R.id.textView);
        textView.setText("获取的随机数为:"+num);
    }
}

客户端通过bindService()方法绑定到服务时,客户端必须提供ServiceConnection接口的实现类,它监视客户端与服务器之间的连接。bindService()方法立即返回,但是当Android系统创建客户端与服务之间的连接时,它调用ServiceConnection接口的onServiceConnected()方法,来发送哭护短用来与服务通信的IBinder对象。
通俗的来将ServiceConnection类是实现前端Activity和服务交互功能的关键,里面定义了前台需要调用那些服务端的功能。
基本的绑定服务就以完成。
运行时出来的效果,当点击按钮会出来获取的随机数:
在这里插入图片描述

发布了7 篇原创文章 · 获赞 10 · 访问量 469

猜你喜欢

转载自blog.csdn.net/jzdcuccess/article/details/105604156