首页 技术 正文
技术 2022年11月10日
0 收藏 560 点赞 3,447 浏览 2751 个字

  Service是一个应用程序组件,没有图形化界面,通常用来处理一些耗时较长的操作,可以用Service更新ContentProvider,发送Intent以及启动系统的通知等等。Service并不是一个单独的进程,也不是一个线程。

  绑定Service和启动Service的区别:

一个Activity启动一个Service之后不能去控制Service的过程和结果,但是实现简单,实现代码如下:

   首先声明一个名为FirstService的类,此类继承自Service类,然后复写Service类的onCreate()、onStartCommand()、onDestroy()方法,如果是首次启动该Service则会调用onCreate()方法和onStartCommand()方法,否则只调用onStartCommand()方法,如果停止该Service则会调用该Service的onDestroy()方法。

  ServiceActivity中启动Service的代码如下:

    Intent intent = new Intent();
    intent.setClass(ServiceActivity.this, FirstService.class);
    startService(intent);

  ServiceActivity中停止Service的代码如下:

    Intent intent = new Intent();
    intent.setClass(ServiceActivity.this, FirstService.class);
    stopService(intent);

绑定Service提供了一种客户端和服务器的模式,通过这种模式Activity可以向Service发送请求,Service可以向Activity返回响应。

  首先在ServiceActivity中声明一个ServiceConnection类的对象conn用来绑定Service和Activity,然后在FirstActivity中声明一个MyBinder类继承自Binder类,复写Binder类的onTransact()方法,当ServiceActivity中获得的FirstService类返回的binder对象执行transact(code,data,reply,flags)方法时。同时执行FirstService类中MyBinder类的onTransact()方法。

  声明ServiceConnection类的对象conn:

    ServiceConnection conn = new ServiceConnection() {
      @Override
      public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
      }
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
        // TODO Auto-generated method stub
        //当绑定成功时,将SecondService返回的binder对象赋值给ServiceActivity的binder对象
        ServiceActivity.this.binder = (Binder)service;
      }
    };

  绑定Service详细代码如下:

    Intent intent = new Intent();
    intent.setClass(ServiceActivity.this, FirstService.class);
    bindService(intent, conn, BIND_AUTO_CREATE);

    ServiceActivity向FirstService发送响应请求和FirstService响应ServiceActivity的请求:

    //生成Parcel对象,Parcel对象就像一个数据集,可以传载数据
    Parcel data = Parcel.obtain();
    data.writeString(“from Activity data”);
    //此时声明的Parcel对象reply中的数据为空
    Parcel reply = Parcel.obtain();
    try {
      //data中是数据,当执行此方法执行时,同时会执行FirstService中的MyBinder对象的onTransact()方法
      binder.transact(0, data, reply, 0);
      String str = reply.readString();
      Log.d(“MyDebug”, str);
    } catch (RemoteException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  FirstService类中MyBinder对象的声明如下,需要复写该类的onTransact()方法:

    class MyBinder extends Binder{

    @Override
    protected boolean onTransact(int code, Parcel data, Parcel reply,int flags) throws RemoteException {
      // TODO Auto-generated method stub
      //将ServiceActivity中传递过来的数据取出
      String str = data.readString();
      Log.d(“MyDebug”, str);
      reply.writeString(“reply Service reply”);
      return super.onTransact(code, data, reply, flags);
    }

  FirstService类中的onBind()方法需返回MyBinder对象,定义如下:

    @Override
    public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return new MyBinder();
    }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295