I have a service which receives a command from the Internet and launches a background thread. This thread is passed a handler from the service (the service is bounded and passed the handler) and sends a message to the handler to take a picture. I'm stuck on the implementation of the handler.
static Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
//TODO: Handle different types of messages
mCamera.takePicture(null, null, MainActivity.this);
}
};
Questions:
You can make a class (Activity/Service) implement Handler.Callback
and create a new Handler for it via new Handler(this)
.
You can change your code as follows:
static Handler handler = new Handler() {
MainActivity mActivity;
@Override
public void handleMessage(Message msg) {
//TODO: Handle different types of messages
if(mActivity != null) {
mActivity.mCamera.takePicture(null, null, mActivity);
}
}
};
void MainActivity::onCreate(Bundle savedState) {
...
handler.mActivity = this;
}
void MainActivity::onDestroy() {
...
handler.mActivity = null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With