Reputation: 157
I'm developing an Android application that installs a service that does some continuously and costly processing (it leverages OpenCV to grab camera images and does some processing on it). I want my activity to connect to and disconnect from the service to do other things on the device and to view the processing (mainly for debugging but I can see other use cases as well).
Problem is I'm not sure how to design this application - I'm also fairly new to Android programming.
Since its a performance critical application I guess I cannot pass bitmaps from the service to the activity so I guess I want to couple the SurfaceView to the service as soon as the activity gets to the foreground.
Something like below - note: its not a complete example, it just shows the flow
public class MainActivity extends Activity {
private OpenCVCameraService mService;
private SurfaceView mSurfaceView;
@Override
public void onCreate() {
// show UI
// start mService with mSurfaceView
}
}
public class OpenCVCameraService extends Service {
private Processing mProcessing;
private SurfaceView mSurfaceView;
@Override
public void onCreate() {
// setup OpenCV camera stuff
// init mProcessing with mSurfaceView
}
}
public class Processing extends Thread {
private VideoCapture mCamera;
private SurfaceView mSurfaceView; // maybe just the SurfaceHolder
Processing(VideoCapture camera, SurfaceView surfaceView) {
mCamera = camera;
mSurfaceView = surfaceView;
}
@Override
public void run() {
mCamera.grab();
mCamera.retrieve(...);
// process retrieved images
// image to bitmap
// What now, how to get bitmap to MainActivity? Passing using intent is a performace hit I guess
// maybe something like if (mSurfaceView != null) { Canvas canvas = mSurfaceView.lockSurface(); ... }
}
}
How would you design such application? Is there anything else I need to consider?
Upvotes: 3
Views: 787
Reputation: 157
Ok, this is how I've done it: I've set the SurfaceView to either the correct view (if app is running) and to null if not (i.e. paused) in the onServiceConnected/onServiceDisconnected of MainActivity. In the service I check whether the view exists or not. It is not the nicest decoupling but it works and as far as I can tell right now the performance is Ok.
Upvotes: 1