Android AIDL bindService後不能呼叫onServiceConnected方法(一種情況)
阿新 • • 發佈:2019-01-24
-
"font-size:18px;">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 );
-
}
- }