Skip to content

Instantly share code, notes, and snippets.

View nalitzis's full-sized avatar
🎯
Focusing

Adolfo Bulfoni nalitzis

🎯
Focusing
  • Amsterdam
View GitHub Profile
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) {
}
}
}
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);
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());
<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
@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;
@nalitzis
nalitzis / SimpleToShort.java
Created February 13, 2013 22:06
Simple to short array java code
/**
* 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));
}
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(){
@nalitzis
nalitzis / wait.java
Last active December 15, 2015 20:59
synchMethod(){
// do something...
callAsycnMethod(callback);
synchronized(this){
try{
this.wait();
}catch(InterruptedException e){}
boolean signal = false;
synchMethod(){
// do something...
callAsycnMethod(callback);
synchronized(this){
while(!signal){
try{
@nalitzis
nalitzis / MeasureButton.java
Created July 12, 2013 20:27
get view size through View.MeasureSpec
private Pair<Integer,Integer> measureButton(){
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.EXACTLY);
mButton.measure(widthMeasureSpec, heightMeasureSpec);
int h = mButton.getMeasuredHeight();
int w = mButton.getMeasuredWidth();
Log.d(TAG, "height of button is :"+h+", width is :"+w);
return new Pair<Integer, Integer>(w,h);
}