当前位置: 移动技术网 > 移动技术>移动开发>Android > Android中Service和Activity相互通信示例代码

Android中Service和Activity相互通信示例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

在android中,activity主要负责前台页面的展示,service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到activity与service之间的通信,本文就给大家详细介绍了关于android中service和activity相互通信的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

activity向service通信

第一种方式:通过mybinder方式调用service方法

mainactivity

public class mainactivity extends activity {

 private myconn conn;
 private mybinder mybinder;//我定义的中间人对象


 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 
 intent intent = new intent(this,banzhengservice.class);
 //连接服务 
 conn = new myconn();
 bindservice(intent, conn, bind_auto_create);
 
 }

 //点击按钮调用服务里面办证的方法
 public void click(view v) {
 
 mybinder.callbanzheng(10000000);
 
 }
 
 
 //监视服务的状态
 private class myconn implements serviceconnection{

 
 //当服务连接成功调用
 @override
 public void onserviceconnected(componentname name, ibinder service) {
  //获取中间人对象
  mybinder = (mybinder) service;
  
 }

 //失去连接
 @override
 public void onservicedisconnected(componentname name) {
  
 }}
 @override
 protected void ondestroy() {
 //当activity 销毁的时候 解绑服务 
 unbindservice(conn);
 super.ondestroy();
 }
 
}

banzhengservice

public class banzhengservice extends service {

 //把我定义的中间人对象返回 
 @override
 public ibinder onbind(intent intent) {
 return new mybinder();
 }

 
 //办证的方法
 public void banzheng(int money){
 if (money>1000) {
  toast.maketext(getapplicationcontext(), "我是领导 把证给你办了", 1).show();
 }else {
  toast.maketext(getapplicationcontext(), "这点钱 还想办事....", 1).show();
 }
 }
 
 
 //[1]定义中间人对象(ibinder)
 
 public class mybinder extends binder{
 
 public void callbanzheng(int money){
  //调用办证的方法
  banzheng(money);
 }
 
 }}

第二种方式:通过接口iservice调用service方法

mainactivity

public class mainactivity extends activity {

 private myconn conn;
 private iservice mybinder;//我定义的中间人对象


 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
 
 intent intent = new intent(this,demoservice.class);
 //连接服务 
 conn = new myconn();
 bindservice(intent, conn, bind_auto_create);
 
 }

 //点击按钮调用服务里面办证的方法
 public void click(view v) {
 
 mybinder.callbanzheng(10000000);
// mybinder.callplaymajiang();
// mybinder.callxisangna();
 
 }
 
 
 //监视服务的状态
 private class myconn implements serviceconnection{

 
 //当服务连接成功调用
 @override
 public void onserviceconnected(componentname name, ibinder service) {
  //获取中间人对象
  mybinder = (iservice) service;
  
 }

 //失去连接
 @override
 public void onservicedisconnected(componentname name) {
  
 }
 
 }
 @override
 protected void ondestroy() {
 //当activity 销毁的时候 解绑服务 
 unbindservice(conn);
 super.ondestroy();
 }
 
}

demoservice

public class demoservice extends service {

 //把我定义的中间人对象返回 
 @override
 public ibinder onbind(intent intent) {
 return new mybinder();
 }

 
 //办证的方法
 public void banzheng(int money){
 if (money>1000) {
  toast.maketext(getapplicationcontext(), "我是领导 把证给你办了", 1).show();
 }else {
  toast.maketext(getapplicationcontext(), "这点钱 还想办事....", 1).show();
 }
 }
 
 //打麻将的方法
 public void playmajiang(){
 system.out.println("陪领导打麻将");
 }
 
 //洗桑拿的方法
 public void 洗桑拿(){
 system.out.println("陪领导洗桑拿");
 
 }
 
 
 
 
 //[1]定义中间人对象(ibinder)
 
 private class mybinder extends binder implements iservice{
 
 public void callbanzheng(int money){
  //调用办证的方法
  banzheng(money);
 }
 
 public void callplaymajiang(){
  //调用playmajiang 的方法
  playmajiang();
  
 }
 
 public void callxisangna(){
  //调用洗桑拿的方法
  洗桑拿();
 }}}

接口iservice

public interface iservice {

 //把领导想暴露的方法都定义在接口里
 public void callbanzheng(int money);
// public void callplaymajiang();
 
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网