package org.lonetin.team;
import org.team.lonetin.IRemoteService;
import org.team.lonetin.IRemoteServiceCallback;
import org.team.lonetin.ISecondary;
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.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
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;
import android.widget.Toast;
public class AndroidAIDLClientActivity extends Activity {
private String tag = "AndroidAIDLClientActivity";
//远端主Service服务接口
IRemoteService mService = null;
//另外一个远端副Service服务接口
ISecondary mSecondaryService = null;
Button mKillButton;
TextView mCallbackText;
private boolean mIsBound;
/**
* 启动Activity,初始化UI
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.bind);
button.setOnClickListener(mBindListener);
button = (Button)findViewById(R.id.unbind);
button.setOnClickListener(mUnbindListener);
button = (Button)this.findViewById(R.id.basicTypes);
button.setOnClickListener(sendBasicTypes);
mKillButton = (Button)findViewById(R.id.kill);
mKillButton.setOnClickListener(mKillListener);
mKillButton.setEnabled(false);
mCallbackText = (TextView)findViewById(R.id.callback);
mCallbackText.setText("Not attached.");
}
/**
* 与主接口的服务进行交互的类。
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.i(tag, "mConnection onServiceConnected()-------");
//通过IDL连接远端Service
mService = IRemoteService.Stub.asInterface(service);
mKillButton.setEnabled(true);
mCallbackText.setText("Attached.");
//检视跟Serivce相连的服务
try {
//参数是Handler
mService.registerCallback(mCallback);
} catch (RemoteException e) {
}
//显示提示框提示用户
Toast.makeText(AndroidAIDLClientActivity.this, R.string.remote_service_connected,
Toast.LENGTH_SHORT).show();
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mService = null;
mKillButton.setEnabled(false);
mCallbackText.setText("Disconnected.");
// As part of the sample, tell the user what happened.
Toast.makeText(AndroidAIDLClientActivity.this, R.string.remote_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};
/**
* Class for interacting with the secondary interface of the service.
*/
private ServiceConnection mSecondaryConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.i(tag, "mSecondaryConnection onServiceConnected()-------");
mSecondaryService = ISecondary.Stub.asInterface(service);
mKillButton.setEnabled(true);
}
public void onServiceDisconnected(ComponentName className) {
mSecondaryService = null;
mKillButton.setEnabled(false);
}
};
private OnClickListener mBindListener = new OnClickListener() {
public void onClick(View v) {
// Establish a couple connections with the service, binding
// by interface names. This allows other applications to be
// installed that replace the remote service by implementing
// the same interface.
Log.i(tag, "mBindListener onClick()-------");
bindService(new Intent(ISecondary.class.getName()),
mSecondaryConnection, Context.BIND_AUTO_CREATE);
bindService(new Intent(IRemoteService.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
mCallbackText.setText("Binding.");
}
};
private OnClickListener mUnbindListener = new OnClickListener() {
public void onClick(View v) {
Log.i(tag, "mUnbindListener onClick()-------");
if (mIsBound) {
// If we have received the service, and hence registered with
// it, then now is the time to unregister.
if (mService != null) {
try {
// 取消bind回调方法,参数是Handler
mService.unregisterCallback(mCallback);
} catch (RemoteException e) {
}
}
// Detach our existing connection.
unbindService(mConnection);
unbindService(mSecondaryConnection);
mKillButton.setEnabled(false);
mIsBound = false;
mCallbackText.setText("Unbinding.");
}
}
};
//杀进程监听器
private OnClickListener mKillListener = new OnClickListener() {
public void onClick(View v) {
int pid = 0;
try {
//Debug.waitForDebugger();
if (mSecondaryService != null) {
pid = mSecondaryService.getPid();
mSecondaryService.killProcess(pid);
}
}catch(DeadObjectException de){
}
catch (RemoteException ex) {
Log.e(tag, "find service process id failed! pid :"+pid, ex);
Toast.makeText(AndroidAIDLClientActivity.this,
R.string.remote_call_failed,
Toast.LENGTH_SHORT).show();
}
try {
if(mService != null){
pid = mService.getPid();
mService.killProcess(pid);
}
mCallbackText.setText("Killed service process.");
} catch(DeadObjectException de){
}catch (RemoteException ex) {
Log.e(tag, "find service process id failed! pid :"+pid, ex);
Toast.makeText(AndroidAIDLClientActivity.this,
R.string.remote_call_failed,
Toast.LENGTH_SHORT).show();
}
//调用unbind事件,断开bind,如果不这么做,两个应用程序是处于bind状态,之前被杀掉的进程会被恢复
mUnbindListener.onClick(v);
}
};
//传参数给service监听器
private OnClickListener sendBasicTypes = new OnClickListener()
{
@Override
public void onClick(View v)
{
if(mService!=null){
//调用基本类型方法,测试传值
try
{
Log.i(tag, "sendBasicTypes");
mService.basicTypes(1, 2, true, 3f, 4d, "String test");
}
catch (RemoteException e)
{
Log.e(tag, "连接失败!", e);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android AIDL进程间通信例子代码
共108个文件
class:47个
java:15个
png:12个
5星 · 超过95%的资源 需积分: 9 60 下载量 176 浏览量
2012-03-23
13:52:02
上传
评论
收藏 160KB RAR 举报
温馨提示
AndroidAIDL进程间通信例子代码,一个客户端调用,一个服务端Service
资源推荐
资源详情
资源评论
收起资源包目录
Android AIDL进程间通信例子代码 (108个子文件)
IRemoteService.aidl 443B
IRemoteService.aidl 443B
IRemoteService.aidl 431B
IRemoteService.aidl 431B
ISecondary.aidl 121B
ISecondary.aidl 121B
ISecondary.aidl 121B
ISecondary.aidl 121B
IRemoteServiceCallback.aidl 115B
IRemoteServiceCallback.aidl 115B
IRemoteServiceCallback.aidl 115B
IRemoteServiceCallback.aidl 115B
resources.ap_ 11KB
resources.ap_ 10KB
AndroidAIDLClient.apk 20KB
AndroidAIDL.apk 18KB
jarlist.cache 119B
jarlist.cache 119B
proguard.cfg 1KB
proguard.cfg 1KB
AndroidAIDLClientActivity.class 4KB
IRemoteService$Stub$Proxy.class 3KB
IRemoteService$Stub$Proxy.class 3KB
IRemoteService$Stub.class 3KB
IRemoteService$Stub.class 3KB
RemoteService$1.class 3KB
AndroidAIDLClientActivity$5.class 2KB
AndroidAIDLClientActivity$1.class 2KB
AndroidAIDLClientActivity$4.class 2KB
ISecondary$Stub.class 2KB
ISecondary$Stub.class 2KB
IRemoteServiceCallback$Stub.class 2KB
IRemoteServiceCallback$Stub.class 2KB
ISecondary$Stub$Proxy.class 2KB
ISecondary$Stub$Proxy.class 2KB
AndroidAIDLClientActivity$3.class 2KB
AndroidAIDLClientActivity$2.class 2KB
RemoteService.class 2KB
Rect.class 2KB
IRemoteServiceCallback$Stub$Proxy.class 2KB
IRemoteServiceCallback$Stub$Proxy.class 2KB
AndroidAIDLClientActivity$6.class 1KB
SecondaryService$1.class 1KB
AndroidAIDLClientActivity$8.class 1KB
Rect$1.class 1KB
AndroidAIDLClientActivity$7.class 1013B
SecondaryService.class 958B
R$string.class 562B
IRemoteService.class 535B
IRemoteService.class 535B
AndroidAIDLActivity.class 534B
R.class 517B
R$id.class 500B
R.class 477B
R$string.class 421B
R$drawable.class 401B
R$drawable.class 401B
R$layout.class 388B
R$layout.class 388B
IRemoteServiceCallback.class 344B
IRemoteServiceCallback.class 344B
BuildConfig.class 341B
BuildConfig.class 341B
ISecondary.class 340B
ISecondary.class 340B
R$attr.class 334B
R$attr.class 334B
.classpath 364B
.classpath 364B
classes.dex 16KB
classes.dex 14KB
AndroidAIDLClientActivity.java 9KB
IRemoteService.java 6KB
IRemoteService.java 6KB
ISecondary.java 3KB
ISecondary.java 3KB
IRemoteServiceCallback.java 2KB
IRemoteServiceCallback.java 2KB
RemoteService.java 2KB
Rect.java 1KB
R.java 1KB
SecondaryService.java 985B
R.java 645B
AndroidAIDLActivity.java 357B
BuildConfig.java 158B
BuildConfig.java 158B
ic_launcher.png 4KB
ic_launcher.png 4KB
ic_launcher.png 4KB
ic_launcher.png 4KB
ic_launcher.png 3KB
ic_launcher.png 3KB
ic_launcher.png 2KB
ic_launcher.png 2KB
ic_launcher.png 2KB
ic_launcher.png 2KB
ic_launcher.png 2KB
ic_launcher.png 2KB
.project 853B
.project 847B
共 108 条
- 1
- 2
IT难
- 粉丝: 19
- 资源: 52
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页