Android中aidl实现Activity与service进行通信和回调
2014-02-14 10:22
344 查看
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
package com.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestAidl
extends Activity
{
String str
= null;
private ImyserviceManager myManager;
Button myButton;
private TextView textView;
private Button button1;
private Button button2;
private ServiceConnection serviceConnection
=new ServiceConnection()
{
public
void onServiceConnected(ComponentName
name, IBinder service)
{
// TODO Auto-generated method stub+
myManager=ImyserviceManager.Stub.asInterface(service);
try
{
Log.i("test-------",myManager.show());
TextView textView=(TextView)findViewById(R.id.text);
textView.setText(str);
myManager.register(new myListener());
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public
void onServiceDisconnected(ComponentName
name)
{
// TODO Auto-generated method stub
}
};
public class myListener
extends Ilisten.Stub
{
public
void change(String a)
throws RemoteException
{
// TODO Auto-generated method stub
button1.setText(a);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bindService(new Intent(TestAidl.this,
RemoteService.class), serviceConnection,
Context.BIND_AUTO_CREATE);
textView=(TextView)findViewById(R.id.text);
button1 =
(Button) findViewById(R.id.b1);
button1.setOnClickListener(new
View.OnClickListener()
{
public
void onClick(View v)
{
try
{
button1.setText(myManager.show());
//myManager.add(1, 2);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button2=
(Button)findViewById(R.id.b2);
button2.setOnClickListener(new
View.OnClickListener()
{
public
void onClick(View v)
{
try
{
myManager.add(2, 3);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
http://blog.chinaunix.net/uid-25677340-id-2854775.html
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
package com.test;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class TestAidl
extends Activity
{
String str
= null;
private ImyserviceManager myManager;
Button myButton;
private TextView textView;
private Button button1;
private Button button2;
private ServiceConnection serviceConnection
=new ServiceConnection()
{
public
void onServiceConnected(ComponentName
name, IBinder service)
{
// TODO Auto-generated method stub+
myManager=ImyserviceManager.Stub.asInterface(service);
try
{
Log.i("test-------",myManager.show());
TextView textView=(TextView)findViewById(R.id.text);
textView.setText(str);
myManager.register(new myListener());
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public
void onServiceDisconnected(ComponentName
name)
{
// TODO Auto-generated method stub
}
};
public class myListener
extends Ilisten.Stub
{
public
void change(String a)
throws RemoteException
{
// TODO Auto-generated method stub
button1.setText(a);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bindService(new Intent(TestAidl.this,
RemoteService.class), serviceConnection,
Context.BIND_AUTO_CREATE);
textView=(TextView)findViewById(R.id.text);
button1 =
(Button) findViewById(R.id.b1);
button1.setOnClickListener(new
View.OnClickListener()
{
public
void onClick(View v)
{
try
{
button1.setText(myManager.show());
//myManager.add(1, 2);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button2=
(Button)findViewById(R.id.b2);
button2.setOnClickListener(new
View.OnClickListener()
{
public
void onClick(View v)
{
try
{
myManager.add(2, 3);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
http://blog.chinaunix.net/uid-25677340-id-2854775.html
相关文章推荐
- Android AIDL线程通信之Service回调客户端Activity
- 使用android中的AIDL让Service与Activity通信(service回调activity)
- Android通信—信使的使用实现Activity与Service的通信
- Android之Activity ContentProvider Service Broadcast,线程间通信,AIDL(一)
- Android bind 回调实现Service向activity传递数据
- Android学习之Activity与Service进行通信的三种方式
- Android之Activity ContentProvider Service Broadcast,线程间通信,AIDL(二)
- Android service 和 client的进程通信和消息回调--AIDL
- Android之Activity ContentProvider Service Broadcast,线程间通信,AIDL(三)
- Android开发学习之路-回调实现Service向activity传递数据
- [实例教程]使用android中的AIDL让Service与Activity通信(service回调activity)
- Android组件---Service,AIDL,Service与Activity之间的通信
- 使用android中的AIDL让Service与Activity通信(service回调activity)
- Android中Service与Activity的通信---回调接口方式
- 安卓跨应用调用Activity,Service并进行通信,AIDL的应用
- 在Activity和Service之间使用Binder和回调接口进行通信
- 使用android中的AIDL让Service与Activity通信(service回调activity)
- 使用android中的AIDL让Service与Activity通信(service回调activity)
- Android 音乐播放器的开发教程(七)运用Broadcast实现service与activity的通信 ----- 小达
- Android:Service:与Activity进行相互通信的本地服务