Last active
August 12, 2019 10:23
-
-
Save ErikHellman/4b6ecc87d1b37c9809d1 to your computer and use it in GitHub Desktop.
Simple LocalBinder demo for Android
This file contains 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
package se.hellsoft.simpleservicecallbackdemo; | |
import android.app.Activity; | |
import android.content.ComponentName; | |
import android.content.Intent; | |
import android.content.ServiceConnection; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.IBinder; | |
import android.os.Message; | |
public class MyActivity extends Activity implements ServiceConnection, MyService.Callback, Handler.Callback { | |
private MyService mService; | |
public static final int CHANGE_BG_MSG = 1001; | |
private Handler mHandler; | |
private int mColor = Color.WHITE; | |
private boolean mSetBg = false; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_my); | |
mHandler = new Handler(getMainLooper(), this); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
bindService(new Intent(this, MyService.class), this, BIND_AUTO_CREATE); | |
mSetBg = true; | |
mHandler.sendEmptyMessageDelayed(CHANGE_BG_MSG, 1000); | |
} | |
@Override | |
protected void onStop() { | |
mService.setCallback(null); | |
unbindService(this); | |
mService = null; | |
mSetBg = false; | |
super.onStop(); | |
} | |
@Override | |
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { | |
mService = ((MyService.LocalBinder) iBinder).getService(); | |
mService.setCallback(this); | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName componentName) { | |
mService = null; | |
} | |
@Override | |
public void onResult(String result) { | |
// TODO Show result | |
} | |
@Override | |
public boolean handleMessage(Message message) { | |
if(message.what == CHANGE_BG_MSG && mSetBg) { | |
findViewById(R.id.watch_view_stub).setBackgroundColor(mColor); | |
mHandler.sendEmptyMessageDelayed(CHANGE_BG_MSG, 1000); | |
} | |
return true; | |
} | |
} |
This file contains 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
package se.hellsoft.simpleservicecallbackdemo; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.Binder; | |
import android.os.IBinder; | |
public class MyService extends Service { | |
private final LocalBinder mLocalBinder = new LocalBinder(); | |
private Callback mCallback; | |
public class LocalBinder extends Binder { | |
public MyService getService() { | |
return MyService.this; | |
} | |
} | |
public interface Callback { | |
void onResult(String result); | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return mLocalBinder; | |
} | |
public void doSomeServiceJob() { | |
// TODO do stuff here... | |
if(mCallback != null) mCallback.onResult("result..."); | |
} | |
@Override | |
public boolean onUnbind(Intent intent) { | |
mCallback = null; | |
return super.onUnbind(intent); | |
} | |
public void setCallback(Callback callback) { | |
mCallback = callback; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment