Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360895
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: 嵌入式

2011-09-16 18:31:11

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. }

阅读(18863) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~