This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static class MyHandler extends Handler{ | |
public void handleMessage(Message msg){ | |
Log.d(TAG, Thread.currentThread().getName()+", "+ msg.obj.toString()); | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e) { | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void bindService(){ | |
Intent intent = new Intent("com.example.testbinder.ClientService"); | |
this.bindService(intent, myConnection, BIND_AUTO_CREATE); | |
} | |
private ServiceConnection myConnection = new ServiceConnection(){ | |
@Override | |
public void onServiceConnected(ComponentName className, IBinder binder) { | |
messenger = new Messenger(binder); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class IncomingHandler extends Handler{ | |
public void handleMessage(Message msg){ | |
Bundle data = msg.getData(); | |
String title = data.getString("TITLE"); | |
int pid = Process.myPid(); | |
Toast.makeText(getApplicationContext(), title + "\nservice pid: "+pid, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
private final Messenger messenger = new Messenger(new IncomingHandler()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<service | |
android:name="com.example.testbinder.ClientService" | |
android:process=":client_service" | |
android:exported="false" | |
> | |
<intent-filter > | |
<action android:name="com.example.testbinder.ClientService"></action> | |
</intent-filter> | |
</service> | |
<service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public IBinder onBind(Intent intent) { | |
return binder; | |
} | |
private final IMultiplier.Stub binder = new IMultiplier.Stub() { | |
@Override | |
public void multiply(final int a, final int b) throws RemoteException { | |
final int result = a*b; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Dealing with big endian streams | |
* @param byte0 | |
* @param byte1 | |
* @return a shrt with the two bytes swapped | |
*/ | |
private static short swapBytes(byte byte0, byte byte1){ | |
return (short)((byte1 & 0xff) << 8 | (byte0 & 0xff)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
synchMethod(){ | |
// do something... | |
callAsycnMethod(callback); | |
//we want to stop here and wait for the callback before returning | |
} | |
private class CallbackImpl implements Callback{ | |
public void onComplete(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
synchMethod(){ | |
// do something... | |
callAsycnMethod(callback); | |
synchronized(this){ | |
try{ | |
this.wait(); | |
}catch(InterruptedException e){} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
boolean signal = false; | |
synchMethod(){ | |
// do something... | |
callAsycnMethod(callback); | |
synchronized(this){ | |
while(!signal){ | |
try{ |