免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1512 | 回复: 0

Android中aidl实现Activity与service进行通信和回调 [复制链接]

论坛徽章:
0
发表于 2011-12-23 03:35 |显示全部楼层
service一直再运行,通过bindService拿到service的代理,并将自己到回调对象注册过去,就能实现调用service中的方法,和在service中调用本地activity到方法。做到了进程间通信。

ImyserviceManager.aidl
  1. package com.test;

  2. import com.test.Ilisten;

  3. interface ImyserviceManager
  4. {
  5.     int add(int a,int b);
  6.     String show();
  7.     void register(Ilisten listen);
  8. }

RemoteService.java

  1. package com.test;

  2. import android.app.Service;
  3. import android.content.Intent;
  4. import android.os.IBinder;
  5. import android.os.RemoteException;
  6. import android.util.Log;

  7. public class RemoteService extends Service
  8. {
  9.     Ilisten myListener = null;
  10.     public class ServiceImpl extends ImyserviceManager.Stub
  11.     {
  12.         public int add(int a,int b)throws RemoteException
  13.         {
  14.             if(myListener != null)
  15.                 myListener.change("this is call back!");
  16.             return (a+b);
  17.         }
  18.         
  19.         public String show()throws RemoteException
  20.         {
  21.             return "hello world!";
  22.         }

  23.         public void register(Ilisten listen) throws RemoteException
  24.         {
  25.             // TODO Auto-generated method stub
  26.             myListener = listen;
  27.         }
  28.     }
  29.     


  30.     @Override
  31.     public IBinder onBind(Intent intent)
  32.     {
  33.         // TODO Auto-generated method stub
  34.         return new ServiceImpl();
  35.     }

  36.     @Override
  37.     public int onStartCommand(Intent intent, int flags, int startId) {
  38.         // TODO Auto-generated method stub
  39.         Log.i("test","I am running .......................");
  40.         return super.onStartCommand(intent, flags, startId);
  41.         
  42.     }
  43.     
  44.     
  45. }

Ilisten.aidl

  1. package com.test;

  2. interface Ilisten
  3. {
  4.     void change(String a);
  5. }

TestAidl.java
  1. package com.test;

  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.os.RemoteException;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.TextView;

  15. public class TestAidl extends Activity
  16. {
  17.     String str = null;
  18.     private ImyserviceManager myManager;
  19.     Button myButton;
  20.     private TextView textView;
  21.     private Button button1;
  22.     private Button button2;
  23.     
  24.     private ServiceConnection serviceConnection =new ServiceConnection()
  25.     {

  26.         public void onServiceConnected(ComponentName name, IBinder service)
  27.         {
  28.             // TODO Auto-generated method stub+
  29.             
  30.             myManager=ImyserviceManager.Stub.asInterface(service);
  31.             try {
  32.                 Log.i("test-------",myManager.show());
  33.                 TextView textView=(TextView)findViewById(R.id.text);
  34.                 textView.setText(str);
  35.                 myManager.register(new myListener());
  36.                 
  37.             } catch (RemoteException e) {
  38.                 // TODO Auto-generated catch block
  39.                 e.printStackTrace();
  40.             }
  41.         }

  42.         public void onServiceDisconnected(ComponentName name)
  43.         {
  44.             // TODO Auto-generated method stub
  45.             
  46.         }
  47.         
  48.     };
  49.     
  50.     public class myListener extends Ilisten.Stub
  51.     {

  52.         public void change(String a) throws RemoteException
  53.         {
  54.             // TODO Auto-generated method stub
  55.             
  56.             button1.setText(a);
  57.             
  58.         }
  59.         
  60.     }
  61.     
  62.     /** Called when the activity is first created. */
  63.     @Override
  64.     public void onCreate(Bundle savedInstanceState)
  65.     {
  66.         super.onCreate(savedInstanceState);
  67.         setContentView(R.layout.main);
  68.         bindService(new Intent(TestAidl.this, RemoteService.class), serviceConnection, Context.BIND_AUTO_CREATE);
  69.   
  70.          textView=(TextView)findViewById(R.id.text);
  71.         
  72.          button1 = (Button) findViewById(R.id.b1);
  73.         
  74.          button1.setOnClickListener(new View.OnClickListener() {
  75.             
  76.             public void onClick(View v)
  77.             {
  78.                 try {
  79.                     button1.setText(myManager.show());
  80.                     //myManager.add(1, 2);
  81.                 } catch (RemoteException e) {
  82.                     // TODO Auto-generated catch block
  83.                     e.printStackTrace();
  84.                 }
  85.             }
  86.         });
  87.         
  88.          button2= (Button)findViewById(R.id.b2);
  89.          button2.setOnClickListener(new View.OnClickListener() {
  90.             
  91.             public void onClick(View v)
  92.             {
  93.                 try {
  94.                     myManager.add(2, 3);
  95.                 } catch (RemoteException e) {
  96.                     // TODO Auto-generated catch block
  97.                     e.printStackTrace();
  98.                 }
  99.             }
  100.         });
  101.     

  102.     }
  103. }

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会

【架构革新 高效可控】2020年12月21日-23日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP