service一直再运行,通过bindService拿到service的代理,并将自己到回调对象注册过去,就能实现调用service中的方法,和在service中调用本地activity到方法。做到了进程间通信。
ImyserviceManager.aidl
- package com.test;
-
-
import com.test.Ilisten;
-
-
interface ImyserviceManager
-
{
-
int add(int a,int b);
-
String show();
-
void register(Ilisten listen);
-
}
RemoteService.java
- package com.test;
-
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.util.Log;
-
-
public class RemoteService extends Service
-
{
-
Ilisten myListener = null;
-
public class ServiceImpl extends ImyserviceManager.Stub
-
{
-
public int add(int a,int b)throws RemoteException
-
{
-
if(myListener != null)
-
myListener.change("this is call back!");
-
return (a+b);
-
}
-
-
public String show()throws RemoteException
-
{
-
return "hello world!";
-
}
-
-
public void register(Ilisten listen) throws RemoteException
-
{
-
// TODO Auto-generated method stub
-
myListener = listen;
-
}
-
}
-
-
-
-
@Override
-
public IBinder onBind(Intent intent)
-
{
-
// TODO Auto-generated method stub
-
return new ServiceImpl();
-
}
-
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
// TODO Auto-generated method stub
-
Log.i("test","I am running .......................");
-
return super.onStartCommand(intent, flags, startId);
-
-
}
-
-
-
}
Ilisten.aidl
- package com.test;
-
-
interface Ilisten
-
{
-
void change(String a);
-
}
TestAidl.java
阅读(18884) | 评论(0) | 转发(1) |