Android进程间通讯

http://developer.android.com/guide/components/aidl.html(这个是Android官网给的AIDL学习向导)

Android进程间通讯采用的是Binder机制,Binder是一个底层驱动,下面这些文章是分析Binder的,,学习Binder无论是对开发APP还是系统开发都是比较有用的,了解底层更能帮助学习

推荐阅读的文章
http://blog.csdn.net/luoshengyang/article/details/6618363#comments
(老罗的博客一直在读,书手里也有一本,写的比较具体)
Android深入浅出之Binder机制
Android Binder设计与实现 – 设计篇

我也做了个demo,一个service端,一个activity,顺带复习下以前老师教的知识,做的时候也遇到过bindService不能触发onServiceConnected的情况,后来发现是没有明确指明AIDL需要的service类名–!
我也在网上看到有人做了比较好的总结么么哒
贴个地址与大家分享http://www.myexception.cn/mobile/1181379.html,为了避免链接失效顺带粘贴出来算了
bindService不能触发onServiceConnected方法
在android项目中用到AIDL,今天碰到了一个诡异的问题,花费了半天的时间终于解决了。具体原因有待细究

bindService( service, connection, BIND_AUTO_CREATE ) 之后一直不调用
connection中的onServiceConnected方法
复查了很多容易出错的问题(有问题的童鞋可以复查下面几条):
1、服务器端的service声明,要和客户端bindService的第一个参数匹配,不然客户端启动不了这个服务。

2、服务器端service必须 return实现AIDL接口ITestService.Stub 的binder
@Override
public IBinder onBind( Intent intent )
{
// TODO Auto-generated method stub
return binder; // 返回AIDL接口实例化对象;
}
3、如果前面两种方法都没有解决,那么很有可能就是我要提到的这个问题了,往下看
我的问题代码如下:

`import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
/**

  • Class Name: SearchClientActivity.java Function:
    *
  • Modifications:
    *
  • @author tm DateTime 2013-1-23 下午4:20:20
  • @version 1.0
    */
    public class SearchClientActivity extends Activity
    {
    private static final String TAG = “SearchClientActivity”;
    private ITestService tService = null;
    // 创建远程调用对象
    private ServiceConnection connection = new ServiceConnection( )
    {
    public void onServiceConnected( ComponentName name, IBinder service )
    {
    // TODO Auto-generated method stub
    // 从远程service中获得AIDL实例化对象
    tService = ITestService.Stub.asInterface( service );
    System.out.println( “Bind Success:” + tService );
    }
    public void onServiceDisconnected( ComponentName name )
    {
    // TODO Auto-generated method stub
    tService = null;
    }
    };
    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
    // TODO Auto-generated method stub
    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );
    Intent service = new Intent( ITestService.class.getName( ) );
    // 绑定AIDL
    bindService( service, connection, BIND_AUTO_CREATE );
    //tService为空 死循环等待异步任务结束
    while ( tService == null )
    {
    Thread.sleep( 500 );
    }
    try
    {
    //在客户端调用服务器端的方法
    List< SearchResultItem > resultItems = tService.getSearchResulet( “” );
    for ( int i = 0; i < resultItems.size( ); i++ )
    {
    Log.i( TAG, resultItems.get( i ).getIndex( ) + resultItems.get( i ).getDetailContent( ) );
    }
    }
    catch ( RemoteException e )
    {
    // TODO Auto-generated catch block
    e.printStackTrace( );
    }
    }
    @Override
    protected void onDestroy( )
    {
    // TODO Auto-generated method stub
    super.onDestroy( );
    unbindService( connection );
    }
    }`

结果一直陷入死循环,不跑onServiceConnected方法。

解决方法:不要在onCreate中等待得到AIDL的接口服务实例,即上面代码中的tService

java.util.List;
1
import android.app.Activity;
import android.content.ComponentName;
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.widget.ListView;
import com.eebbk.searchclientaidl.R;
public class SearchClientAIDLActivity extends Activity {
private static final String TAG = “SearchClientAIDLActivity”;
private ITestService tService;
//创建远程调用对象
private ServiceConnection connection = new ServiceConnection(){
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println(” onServiceConnected “);
//从远程service中获得AIDL实例化对象
tService = ITestService.Stub.asInterface(service);
System.out.println(“Bind Success:”+tService);
new Thread( new Runnable( )
{
@Override
public void run( )
{
Log.v( TAG, “开启新线程!” );
getResults( );
}
} ).start( );
}
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
tService = null;
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent service = new Intent( ITestService.class.getName());
//绑定AIDL
bindService(service, connection, BIND_AUTO_CREATE);
}
private void getResults()
{
try
{
List< SearchResultItem > resultItems = tService.getSearchResulet( “” );
for ( int i = 0; i < resultItems.size( ); i++ )
{
Log.i( TAG, resultItems.get( i ).getIndex( ) + resultItems.get( i ).getDetailContent( ) );
}
}
catch ( RemoteException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy( )
{
// TODO Auto-generated method stub
super.onDestroy( );
unbindService( connection );
}
}

还有再推荐一篇老罗分析的bindservice启动过程
http://blog.csdn.net/luoshengyang/article/details/6745181
最后留下自己写的demo,http://pan.baidu.com/s/1qWuL7nI源码懒贴了比较简单感兴趣自己下载了玩玩吧,注意先运行服务端
image