Android中Service与Activity之间通信的几种方式

Android中Service与Activity之间通信的几种方式,第1张

概述在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间通信,我们一般在Activity中启动后台Service,通过Intent来启动,In

在AndroID中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activity中启动后台Service,通过Intent来启动,Intent中我们可以传递数据给Service,而当我们Service执行某些 *** 作之后想要更新UI线程,我们应该怎么做呢?接下来我就介绍两种方式来实现Service与Activity之间的通信问题

1、通过Binder对象

当Activity通过调用bindService(Intent service,ServiceConnection conn,int flags),我们可以得到一个Service的一个对象实例,然后我们就可以访问Service中的方法,我们还是通过一个例子来理解一下吧,一个模拟下载的小例子,带大家理解一下通过Binder通信的方式

首先我们新建一个工程Communication,然后新建一个Service类

package com.example.communication; import androID.app.Service; import androID.content.Intent; import androID.os.Binder; import androID.os.IBinder;  public class MsgService extends Service {  /**   * 进度条的最大值   */  public static final int MAX_PROGRESS = 100;  /**   * 进度条的进度值   */  private int progress = 0;   /**   * 增加get()方法,供Activity调用   * @return 下载进度   */  public int getProgress() {   return progress;  }   /**   * 模拟下载任务,每秒钟更新一次   */  public voID startDownLoad(){   new Thread(new Runnable() {        @OverrIDe    public voID run() {     while(progress < MAX_PROGRESS){      progress += 5;      try {       Thread.sleep(1000);      } catch (InterruptedException e) {       e.printstacktrace();      }           }    }   }).start();  }    /**   * 返回一个Binder对象   */  @OverrIDe  public IBinder onBind(Intent intent) {   return new MsgBinder();  }    public class MsgBinder extends Binder{   /**    * 获取当前Service的实例    * @return    */   public MsgService getService(){    return MsgService.this;   }  }  }

上面的代码比较简单,注释也比较详细,最基本的Service的应用了,相信你看得懂的,我们调用startDownLoad()方法来模拟下载任务,然后每秒更新一次进度,但这是在后台进行中,我们是看不到的,所以有时候我们需要他能在前台显示下载的进度问题,所以我们接下来就用到Activity了

Intent intent = new Intent("com.example.communication.MSG_ACTION"); bindService(intent,conn,Context.BIND_auto_CREATE); 

通过上面的代码我们就在Activity绑定了一个Service,上面需要一个ServiceConnection对象,它是一个接口,我们这里使用了匿名内部类

ServiceConnection conn = new ServiceConnection() {      @OverrIDe   public voID onServicedisconnected(Componentname name) {       }      @OverrIDe   public voID onServiceConnected(Componentname name,IBinder service) {    //返回一个MsgService对象    msgService = ((MsgService.MsgBinder)service).getService();       }  };

在onServiceConnected(Componentname name,IBinder service) 回调方法中,返回了一个MsgService中的Binder对象,我们可以通过getService()方法来得到一个MsgService对象,然后可以调用MsgService中的一些方法,Activity的代码如下

package com.example.communication; import androID.app.Activity; import androID.content.Componentname; import androID.content.Context; import androID.content.Intent; import androID.content.ServiceConnection; import androID.os.Bundle; import androID.os.IBinder; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.Progressbar;  public class MainActivity extends Activity {  private MsgService msgService;  private int progress = 0;  private Progressbar mProgressbar;     @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.activity_main);         //绑定Service   Intent intent = new Intent("com.example.communication.MSG_ACTION");   bindService(intent,Context.BIND_auto_CREATE);         mProgressbar = (Progressbar) findVIEwByID(R.ID.progressbar1);   button mbutton = (button) findVIEwByID(R.ID.button1);   mbutton.setonClickListener(new OnClickListener() {        @OverrIDe    public voID onClick(VIEw v) {     //开始下载     msgService.startDownLoad();     //监听进度     ListenProgress();    }   });     }     /**   * 监听进度,每秒钟获取调用MsgService的getProgress()方法来获取进度,更新UI   */  public voID ListenProgress(){   new Thread(new Runnable() {        @OverrIDe    public voID run() {     while(progress < MsgService.MAX_PROGRESS){      progress = msgService.getProgress();      mProgressbar.setProgress(progress);      try {       Thread.sleep(1000);      } catch (InterruptedException e) {       e.printstacktrace();      }     }         }   }).start();  }    ServiceConnection conn = new ServiceConnection() {   @OverrIDe   public voID onServicedisconnected(Componentname name) {       }      @OverrIDe   public voID onServiceConnected(Componentname name,IBinder service) {    //返回一个MsgService对象    msgService = ((MsgService.MsgBinder)service).getService();       }  };   @OverrIDe  protected voID onDestroy() {   unbindService(conn);   super.onDestroy();  }   } 

其实上面的代码我还是有点疑问,就是监听进度变化的那个方法我是直接在线程中更新UI的,不是说不能在其他线程更新UI *** 作吗,可能是Progressbar比较特殊吧,我也没去研究它的源码,知道的朋友可以告诉我一声,谢谢!

上面的代码就完成了在Service更新UI的 *** 作,可是你发现了没有,我们每次都要主动调用getProgress()来获取进度值,然后隔一秒在调用一次getProgress()方法,你会不会觉得很被动呢?可不可以有一种方法当Service中进度发生变化主动通知Activity,答案是肯定的,我们可以利用回调接口实现Service的主动通知,不理解回调方法的可以看

https://www.oudahe.com/p/26536/

新建一个回调接口

public interface OnProgressListener {  voID onProgress(int progress); } 

MsgService的代码有一些小小的改变,为了方便大家看懂,我还是将所有代码贴出来

package com.example.communication;  import androID.app.Service; import androID.content.Intent; import androID.os.Binder; import androID.os.IBinder;  public class MsgService extends Service {  /**   * 进度条的最大值   */  public static final int MAX_PROGRESS = 100;  /**   * 进度条的进度值   */  private int progress = 0;    /**   * 更新进度的回调接口   */  private OnProgressListener onProgressListener;      /**   * 注册回调接口的方法,供外部调用   * @param onProgressListener   */  public voID setonProgressListener(OnProgressListener onProgressListener) {   this.onProgressListener = onProgressListener;  }   /**   * 增加get()方法,供Activity调用   * @return 下载进度   */  public int getProgress() {   return progress;  }   /**   * 模拟下载任务,每秒钟更新一次   */  public voID startDownLoad(){   new Thread(new Runnable() {        @OverrIDe    public voID run() {     while(progress < MAX_PROGRESS){      progress += 5;            //进度发生变化通知调用方      if(onProgressListener != null){       onProgressListener.onProgress(progress);      }            try {       Thread.sleep(1000);      } catch (InterruptedException e) {       e.printstacktrace();      }           }    }   }).start();  }    /**   * 返回一个Binder对象   */  @OverrIDe  public IBinder onBind(Intent intent) {   return new MsgBinder();  }    public class MsgBinder extends Binder{   /**    * 获取当前Service的实例    * @return    */   public MsgService getService(){    return MsgService.this;   }  }  } 

Activity中的代码如下

package com.example.communication;  import androID.app.Activity; import androID.content.Componentname; import androID.content.Context; import androID.content.Intent; import androID.content.ServiceConnection; import androID.os.Bundle; import androID.os.IBinder; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.Progressbar;  public class MainActivity extends Activity {  private MsgService msgService;  private Progressbar mProgressbar;     @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.activity_main);         //绑定Service   Intent intent = new Intent("com.example.communication.MSG_ACTION");   bindService(intent,Context.BIND_auto_CREATE);         mProgressbar = (Progressbar) findVIEwByID(R.ID.progressbar1);   button mbutton = (button) findVIEwByID(R.ID.button1);   mbutton.setonClickListener(new OnClickListener() {        @OverrIDe    public voID onClick(VIEw v) {     //开始下载     msgService.startDownLoad();    }   });     }  ServiceConnection conn = new ServiceConnection() {   @OverrIDe   public voID onServicedisconnected(Componentname name) {       }      @OverrIDe   public voID onServiceConnected(Componentname name,IBinder service) {    //返回一个MsgService对象    msgService = ((MsgService.MsgBinder)service).getService();        //注册回调接口来接收下载进度的变化    msgService.setonProgressListener(new OnProgressListener() {          @OverrIDe     public voID onProgress(int progress) {      mProgressbar.setProgress(progress);           }    });       }  };   @OverrIDe  protected voID onDestroy() {   unbindService(conn);   super.onDestroy();  } } 

用回调接口是不是更加的方便呢,当进度发生变化的时候Service主动通知Activity,Activity就可以更新UI *** 作了

2、通过broadcast(广播)的形式

当我们的进度发生变化的时候我们发送一条广播,然后在Activity的注册广播接收器,接收到广播之后更新Progressbar,代码如下

package com.example.communication; import androID.app.Activity; import androID.content.broadcastReceiver; import androID.content.Context; import androID.content.Intent; import androID.content.IntentFilter; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; import androID.Widget.Progressbar;  public class MainActivity extends Activity {  private Progressbar mProgressbar;  private Intent mIntent;  private MsgReceiver msgReceiver;     @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.activity_main);      //动态注册广播接收器   msgReceiver = new MsgReceiver();   IntentFilter intentFilter = new IntentFilter();   intentFilter.addAction("com.example.communication.RECEIVER");   registerReceiver(msgReceiver,intentFilter);         mProgressbar = (Progressbar) findVIEwByID(R.ID.progressbar1);   button mbutton = (button) findVIEwByID(R.ID.button1);   mbutton.setonClickListener(new OnClickListener() {        @OverrIDe    public voID onClick(VIEw v) {     //启动服务     mIntent = new Intent("com.example.communication.MSG_ACTION");     startService(mIntent);    }   });     }     @OverrIDe  protected voID onDestroy() {   //停止服务   stopService(mIntent);   //注销广播   unregisterReceiver(msgReceiver);   super.onDestroy();  }    /**   * 广播接收器   * @author len   *   */  public class MsgReceiver extends broadcastReceiver{    @OverrIDe   public voID onReceive(Context context,Intent intent) {    //拿到进度,更新UI    int progress = intent.getIntExtra("progress",0);    mProgressbar.setProgress(progress);   }     }  } 
package com.example.communication;  import androID.app.Service; import androID.content.Intent; import androID.os.IBinder;  public class MsgService extends Service {  /**   * 进度条的最大值   */  public static final int MAX_PROGRESS = 100;  /**   * 进度条的进度值   */  private int progress = 0;    private Intent intent = new Intent("com.example.communication.RECEIVER");     /**   * 模拟下载任务,每秒钟更新一次   */  public voID startDownLoad(){   new Thread(new Runnable() {        @OverrIDe    public voID run() {     while(progress < MAX_PROGRESS){      progress += 5;            //发送Action为com.example.communication.RECEIVER的广播      intent.putExtra("progress",progress);      sendbroadcast(intent);            try {       Thread.sleep(1000);      } catch (InterruptedException e) {       e.printstacktrace();      }           }    }   }).start();  }      @OverrIDe  public int onStartCommand(Intent intent,int flags,int startID) {   startDownLoad();   return super.onStartCommand(intent,flags,startID);  }     @OverrIDe  public IBinder onBind(Intent intent) {   return null;  }   }

总结:

Activity调用bindService (Intent service,int flags)方法,得到Service对象的一个引用,这样Activity可以直接调用到Service中的方法,如果要主动通知Activity,我们可以利用回调方法  Service向Activity发送消息,可以使用广播,当然Activity要注册相应的接收器。比如Service要向多个Activity发送同样的消息的话,用这种方法就更好

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android中Service与Activity之间通信的几种方式全部内容,希望文章能够帮你解决Android中Service与Activity之间通信的几种方式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147067.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存