提交 306ed980 编写于 作者: A Alexander Smorkalov

Issue #2284 BaseLoaderCallback requires use of Activity fixed.

上级 a9f36dfc
......@@ -9,3 +9,52 @@ Base Loader Callback Interface implementation
.. image:: img/AndroidAppUsageModel.png
Using in Java Activity
----------------------
There is a very base code snippet implementing the async initialization with BaseLoaderCallback. See the "15-puzzle" OpenCV sample for details.
.. code-block:: java
:linenos:
public class MyActivity extends Activity implements HelperCallbackInterface
{
private BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Create and set View
mView = new puzzle15View(mAppContext);
setContentView(mView);
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
/** Call on every application resume **/
@Override
protected void onResume()
{
Log.i(TAG, "called onResume");
super.onResume();
Log.i(TAG, "Trying to load OpenCV library");
if (!OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_2, this, mOpenCVCallBack))
{
Log.e(TAG, "Cannot connect to OpenCV Manager");
}
}
Using in Service
----------------
Default BaseLoaderCallback implementation treat application context as Activity and calls Activity.finish() method to exit in case of initialization failure.
To override this behavior you need to override finish() method of BaseLoaderCallback class and implement your own finalization method.
......@@ -112,6 +112,8 @@ There is a very base code snippet implementing the async initialization. It show
It this case application works with OpenCV Manager in asynchronous fashion. ``OnManagerConnected`` callback will be called in UI thread, when initialization finishes.
Please note, that it is not allowed to use OpenCV calls or load OpenCV-dependent native libs before invoking this callback.
Load your own native libraries that depend on OpenCV after the successful OpenCV initialization.
Default BaseLoaderCallback implementation treat application context as Activity and calls Activity.finish() method to exit in case of initialization failure.
To override this behavior you need to override finish() method of BaseLoaderCallback class and implement your own finalization method.
Application development with static initialization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......
......@@ -2,6 +2,7 @@ package org.opencv.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
......@@ -11,7 +12,7 @@ import android.util.Log;
*/
public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
public BaseLoaderCallback(Activity AppContext) {
public BaseLoaderCallback(Context AppContext) {
mAppContext = AppContext;
}
......@@ -34,7 +35,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
MarketErrorMessage.setCancelable(false); // This blocks the 'BACK' button
MarketErrorMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
finish();
}
});
MarketErrorMessage.show();
......@@ -43,7 +44,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
case LoaderCallbackInterface.INSTALL_CANCELED:
{
Log.d(TAG, "OpenCV library instalation was canceled by user");
mAppContext.finish();
finish();
} break;
/** Application is incompatible with this version of OpenCV Manager. Possibly, a service update is required. **/
case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION:
......@@ -55,7 +56,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
IncomatibilityMessage.setCancelable(false); // This blocks the 'BACK' button
IncomatibilityMessage.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
finish();
}
});
IncomatibilityMessage.show();
......@@ -71,7 +72,7 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
InitFailedDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
mAppContext.finish();
finish();
}
});
......@@ -130,6 +131,11 @@ public abstract class BaseLoaderCallback implements LoaderCallbackInterface {
}
}
protected Activity mAppContext;
void finish()
{
((Activity) mAppContext).finish();
}
protected Context mAppContext;
private final static String TAG = "OpenCVLoader/BaseLoaderCallback";
}
#!/usr/bin/python
import os
import shutil
for f in os.listdir("."):
shutil.copyfile(f, os.path.join("../../../../../../modules/java/generator/src/java/", "android+" + f));
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册