1. 程式人生 > >android使用aidl實現程序間通訊的例項

android使用aidl實現程序間通訊的例項

//這一步啟動服務
        this.bindService(new Intent("com.wyj.aidlservice"), this.serviceConnection, BIND_AUTO_CREATE);
        
        btn = (Button)findViewById(R.id.button1);  
        btn.setOnClickListener(this);  
        
    }
    
    private ServiceConnection serviceConnection = new ServiceConnection() {    
        public void onServiceConnected(ComponentName className, IBinder service) {    
        Log.i(TAG, "onServiceConnected");
        aidltest = IAidlTest.Stub.asInterface(service);  
        }    

//這個介面要實現,不然後報錯
        public void onServiceDisconnected(ComponentName className) {    
        Log.i(TAG, "onServiceDisconnected");
        aidltest = null;
        }    
};




@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try {
Log.i(TAG, "onClick hello");
aidltest.print("hello");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}