服务与活动的相互通信怎么在Android项目中实现

  介绍

今天就跟大家聊聊有关服务与活动的相互通信怎么在Android项目中实现,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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

<强> 1,通过粘合剂对象

当活动通过调用bindService(目的服务,ServiceConnection康涅狄格州,int旗帜),我们可以得到一个服务的一个对象实例,然后我们就可以访问服务中的方法,我们还是通过一个例子来理解一下吧,一个模拟下载的小例子,带大家理解一下通过粘合剂通信的方式

首先我们新建一个工程沟通,然后新建一个服务类

包com.example.communication;
  进口android.app.Service;
  进口android.content.Intent;
  进口android.os.Binder;
  进口android.os.IBinder;
  
  公开课MsgService延伸服务{/* *
  *进度条的最大值
  */公共静态最终int MAX_PROGRESS=100;/* *
  *进度条的进度值
  */私人int进步=0;/* *
  *增加get()方法,供活动调用
  * @return下载进度
  */公共int getProgress () {
  返回进展;
  }/* *
  *模拟下载任务,每秒钟更新一次
  */公共空间startDownLoad () {
  新线程(新Runnable () {
  
  @Override
  公共空间run () {
  而(进展& lt;MAX_PROGRESS) {
  进步+=5;
  尝试{
  thread . sleep (1000);
  }捕捉(InterruptedException e) {
  e.printStackTrace ();
  }
  
  }
  }
  }) .start ();
  }/* *
  *返回一个活页夹对象
  */@Override
  公共内部>目的意图=new意图(“com.example.communication.MSG_ACTION");
  bindService(意图,康涅狄格州,Context.BIND_AUTO_CREATE);

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

 ServiceConnection康涅狄格州=new ServiceConnection () {
  
  @Override
  公共空间>包com.example.communication;
  进口android.app.Activity;
  进口android.content.ComponentName;
  进口android.content.Context;
  进口android.content.Intent;
  进口android.content.ServiceConnection;
  进口android.os.Bundle;
  进口android.os.IBinder;
  进口android.view.View;
  进口android.view.View.OnClickListener;
  进口android.widget.Button;
  进口android.widget.ProgressBar;
  
  公开课MainActivity延伸活动{
  私人MsgService MsgService;
  私人int进步=0;
  私人ProgressBar mProgressBar;
  
  
  @Override
  保护空白>公共接口>包com.example.communication;
  
  进口android.app.Service;
  进口android.content.Intent;
  进口android.os.Binder;
  进口android.os.IBinder;
  
  公开课MsgService延伸服务{/* *
  *进度条的最大值
  */公共静态最终int MAX_PROGRESS=100;/* *
  *进度条的进度值
  */私人int进步=0;/* *
  *更新进度的回调接口
  */私人com.example.communication>包;
  
  进口android.app.Activity;
  进口android.content.ComponentName;
  进口android.content.Context;
  进口android.content.Intent;
  进口android.content.ServiceConnection;
  进口android.os.Bundle;
  进口android.os.IBinder;
  进口android.view.View;
  进口android.view.View.OnClickListener;
  进口android.widget.Button;
  进口android.widget.ProgressBar;
  
  公开课MainActivity延伸活动{
  私人MsgService MsgService;
  私人ProgressBar mProgressBar;
  
  
  @Override
  保护空白>包com.example.communication;
  进口android.app.Activity;
  进口android.content.BroadcastReceiver;
  进口android.content.Context;
  进口android.content.Intent;
  进口android.content.IntentFilter;
  进口android.os.Bundle;
  进口android.view.View;
  进口android.view.View.OnClickListener;
  进口android.widget.Button;
  进口android.widget.ProgressBar;
  
  公开课MainActivity延伸活动{
  私人ProgressBar mProgressBar;
  私人意图mIntent;
  私人MsgReceiver MsgReceiver;
  
  
  @Override
  保护空白>包com.example.communication;
  
  进口android.app.Service;
  进口android.content.Intent;
  进口android.os.IBinder;
  
  公开课MsgService延伸服务{/* *
  *进度条的最大值
  */公共静态最终int MAX_PROGRESS=100;/* *
  *进度条的进度值
  */私人int进步=0;
  
  私人的目的意图=new意图(“com.example.communication.RECEIVER");/* *
  *模拟下载任务,每秒钟更新一次
  */公共空间startDownLoad () {
  新线程(新Runnable () {
  
  @Override
  公共空间run () {
  而(进展& lt;MAX_PROGRESS) {
  进步+=5;//发送行动为com.example.communication.RECEIVER的广播
  intent.putExtra (“progress"、进步);
  sendBroadcast(意图);
  
  尝试{
  thread . sleep (1000);
  }捕捉(InterruptedException e) {
  e.printStackTrace ();
  }
  
  }
  }
  }) .start ();
  }
  
  
  
  @Override
  公共int>看完上述内容,你们对服务与活动的相互通信怎么在Android项目中实现有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

服务与活动的相互通信怎么在Android项目中实现