bindService不能触发onServiceConnected方法
来源:互联网 发布:淘宝开店显示已有店铺 编辑:程序博客网 时间:2024/06/05 00:59
在android项目中用到AIDL,今天碰到了一个诡异的问题,花费了半天的时间终于解决了。具体原因有待细究
bindService( service, connection, BIND_AUTO_CREATE ) 之后一直不调用
connection中的onServiceConnected方法
复查了很多容易出错的问题(有问题的童鞋可以复查下面几条):
1、服务器端的service声明,要和客户端bindService的第一个参数匹配,不然客户端启动不了这个服务。
<service
android:name="com.eebbk.keywordsearch.ExamCommentService"
android:process=":remote" >
<intent-filter>
<!-- AIDL完整路径名。必须指明,客户端能够通过AIDL类名查找到它的实现类 -->
<action android:name="com.eebbk.keywordsearch.ITestService" />
</intent-filter>
</service>
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 stubtService = null;}};@Overrideprotected void onCreate( Bundle savedInstanceState ){// TODO Auto-generated method stubsuper.onCreate( savedInstanceState );setContentView( R.layout.main );Intent service = new Intent( ITestService.class.getName( ) );// 绑定AIDLbindService( 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 blocke.printStackTrace( );}}@Overrideprotected void onDestroy( ){// TODO Auto-generated method stubsuper.onDestroy( );unbindService( connection );}}
结果一直陷入死循环,不跑onServiceConnected方法。
解决方法:不要在onCreate中等待得到AIDL的接口服务实例,即上面代码中的tService
import java.util.List;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 stubSystem.out.println(" onServiceConnected ");//从远程service中获得AIDL实例化对象tService = ITestService.Stub.asInterface(service);System.out.println("Bind Success:"+tService);new Thread( new Runnable( ){@Overridepublic void run( ){Log.v( TAG, "开启新线程!" );getResults( );}} ).start( );}public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubtService = 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());//绑定AIDLbindService(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 blocke.printStackTrace();} } @Overrideprotected void onDestroy( ){// TODO Auto-generated method stubsuper.onDestroy( );unbindService( connection );}}
bindService的具体流程可以参考
http://blog.csdn.net/luoshengyang/article/details/6745181
- bindService不能触发onServiceConnected方法
- bindService不能触发onServiceConnected方法的原因
- bindService不能触发onServiceConnected方法的原因
- Android Service在bindService不能触发onServiceConnected方法
- Android Service在bindService不能触发onServiceConnected方法
- Android AIDL bindService后不能调用onServiceConnected方法(一种情况)
- bindService后,onServiceConnected方法没有执行
- android service bindService onServiceConnected没有调用
- bindService不调用onServiceConnected的问题
- $(window).scroll() 方法不能触发
- 不回调ServiceConnection中onServiceConnected()方法的问题
- 调用了bindService()之后,程序执行了Service里的onBind(),但是为什么不执行ServiceConnection对象接口onServiceConnected()呢?
- js 与 native 交互时有时候不能触发shouldStartLoadWithRequest方法
- 解决<span>标签内容改变,不能触发方法的问题
- onServiceConnected,onServiceDisconnected
- onServiceConnected,onServiceDisconnected
- bindService 和startService 终止方法
- bindService
- 一个基于xml的超级orm框架-世界是由懒人创造的。
- MyEclipse生成Javadoc帮助文档
- linux下添加,删除,修改,查看用户和用户组
- POJ 2151 Check the difficulty of problems
- 运行shell:windows命令,及显示桌面.scf的问题
- bindService不能触发onServiceConnected方法
- UML 学习第一章
- 学习Opencv与MFC混合编程之---为播放AVI视频添加滑动条
- Python 笔记(1)input与raw_input
- freemarker学习记录
- 【OpenCV学习】抠图
- 如何搭建本地svn服务器
- CATransition 动画的参考解析
- 搜狗实验室数字资源