最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

android 的Activity和Service之间的通信

来源:动视网 责编:小OO 时间:2025-09-26 18:08:00
文档

android 的Activity和Service之间的通信

android的Activity和Service之间的通信在android中Activity负责前台界面展示,service负责后台的需要长期运行的任务。Activity和Service之间的通信主要由IBinder负责。在需要和Service通信的Activity中实现ServiceConnection接口,并且实现其中的onServiceConnected和onServiceDisconnected方法。然后在这个Activity中还要通过如下代码绑定服务:Java代码1.Intent i
推荐度:
导读android的Activity和Service之间的通信在android中Activity负责前台界面展示,service负责后台的需要长期运行的任务。Activity和Service之间的通信主要由IBinder负责。在需要和Service通信的Activity中实现ServiceConnection接口,并且实现其中的onServiceConnected和onServiceDisconnected方法。然后在这个Activity中还要通过如下代码绑定服务:Java代码1.Intent i
android 的Activity和Service之间的通信

在android中Activity负责前台界面展示,service负责后台的需要长期运行的任务。Activity和Service之间的通信主要由IBinder负责。在需要和Service通信的Activity中实现ServiceConnection接口,并且实现其中的onServiceConnected和onServiceDisconnected方法。然后在这个Activity中还要通过如下代码绑定服务:

Java代码

1.Intent intent = new Intent().setClass( this , IHRService.class );   

2.bindService( intent , this , Context.BIND_AUTO_CREATE );  

Intent intent = new Intent().setClass( this , IHRService.class );

bindService( intent , this , Context.BIND_AUTO_CREATE );

 当调用bindService方法后就会回调Activity的onServiceConnected,在这个方法中会向Activity中传递一个IBinder的实例,Acitity需要保存这个实例。代码如下:

Java代码

1.public void onServiceConnected( ComponentName inName , IBinder serviceBinder) {   

2.    if ( inName.getShortClassName().endsWith( "IHRService" ) ) {   

3.    try {   

4.        this.serviceBinder= serviceBinder;   

5.        mService = ( (IHRService.MyBinder) serviceBinder).getService();   

6.        //mTracker = mService.mConfiguration.mTracker;   

7.        } catch (Exception e) {}   

8.               

9.    }   

10.}  

public void onServiceConnected( ComponentName inName , IBinder serviceBinder) {

     if ( inName.getShortClassName().endsWith( "IHRService" ) ) {

    try {

        this.serviceBinder= serviceBinder;

        mService = ( (IHRService.MyBinder) serviceBinder).getService();

        //mTracker = mService.mConfiguration.mTracker;

        } catch (Exception e) {}

            

    }

}

 

在Service中需要创建一个实现IBinder的内部类(这个内部类不一定在Service中实现,但必须在Service中创建它)。

Java代码

1.public class MyBinder extends Binder {   

2.//此方法是为了可以在Acitity中获得服务的实例   

3.    public IHRService getService() {   

4.        return IHRService.this;   

5.    }   

6.//这个方法主要是接收Activity发向服务的消息,data为发送消息时向服务传入的对象,replay是由服务返回的对象   

7.    public boolean onTransact( int code , Parcel data , Parcel reply , int flags ) {   

8.        //called when client calls transact on returned Binder   

9.        return handleTransactions( code , data , reply , flags );   

10.    }   

11.  

12.}  

public class MyBinder extends Binder {

//此方法是为了可以在Acitity中获得服务的实例

    public IHRService getService() {

        return IHRService.this;

    }

//这个方法主要是接收Activity发向服务的消息,data为发送消息时向服务传入的对象,replay是由服务返回的对象

    public boolean onTransact( int code , Parcel data , Parcel reply , int flags ) {

        //called when client calls transact on returned Binder

        return handleTransactions( code , data , reply , flags );

    }

}

  

然后在Service中创建这个类的实例:

Java代码

1.public IBinder onBind( Intent intent ) {   

2.    IBinder result = null;   

3.    if ( null == result ) result = new MyBinder() ;   

4.    return result;   

5.}  

public IBinder onBind( Intent intent ) {

    IBinder    result = null;

    if ( null == result ) result = new MyBinder() ;

    return result;

}

 这时候如果Activity向服务发送消息,就可以调用如下代码向服务端发送消息:

Java代码

1.inSend = Parcel.obtain();   

2.serviceBinder.transact( inCode , inSend , null , IBinder.FLAG_ONEWAY );  

inSend = Parcel.obtain();

serviceBinder.transact( inCode , inSend , null , IBinder.FLAG_ONEWAY );

 

这种方式是只向服务端发送消息,没有返回值的。如果需要从服务端返回某些值则可用如下代码:

Java代码  

1.result = Parcel.obtain();   

2.serviceBinder.transact( inCode , inSend , result , 0 );   

3.return result;  

result = Parcel.obtain();

serviceBinder.transact( inCode , inSend , result , 0 );

return result;

发送消息后IBinder接口中的onTransact将会被调用。在服务中如果有结果返回(比如下载数据)则将结果写入到result参数中。在Activity中从result中读取服务执行的结果。

 

上面只是描述了如何由Acitity向Service发送消息,如果Service向Activity发送消息则可借助于BroadcastReceiver实现,BroadcastReceiver比较简单,前面在将Service中已有提及。

文档

android 的Activity和Service之间的通信

android的Activity和Service之间的通信在android中Activity负责前台界面展示,service负责后台的需要长期运行的任务。Activity和Service之间的通信主要由IBinder负责。在需要和Service通信的Activity中实现ServiceConnection接口,并且实现其中的onServiceConnected和onServiceDisconnected方法。然后在这个Activity中还要通过如下代码绑定服务:Java代码1.Intent i
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top