How can I catch the static java.lang.UnsatisfiedLinkError from Android and show the user a better error message?

My application loads various shared objects as it is created. I would like to catch errors thrown because shared objects were not present on the device and displaying an error message to the user. How do you achieve this?

I can catch java.lang.UnsatisfiedLinkError

this way

static
{
    try
    {
        System.loadLibrary("MyApplication");
    }
    catch(java.lang.UnsatisfiedLinkError e)
    {
        if(e.getMessage().contains("libSharedObject"))
        {
            Log.e( TAG, "This device does not support ..." );
        }
        else
        {
            throw e;
        }
    }
}

      

But Toast.makeText(...).show()

other message boxes of the application will not work either, because the application will die in onCreate()

due to the previous error.

Is there a way to change the default system error message "Sorry, an error occurred .."? Or a way to display an error message with another process or Android OS?

+3


source to share


2 answers


I found an answer using this answer . I catch the exception in the block static {}

, set a member variable to indicate the error and the message, then create a new thread that displays the error using Toast and uses a Looper to invoke the message loop on that thread. I need to sleep on the main thread for a while, but before I allow the program to crash.



static boolean mWasError = false;
static String mErrorMessage = "";

static
{
    try
    {
        System.loadLibrary("MyApplication");
    }
    catch(java.lang.UnsatisfiedLinkError e)
    {
        if(e.getMessage().contains("libOpenCL"))
        {
            Log.e( TAG, "This device does not support OpenCL" );
            mWasError = true;
            mErrorMessage = "This device does not support OpenCL";
        }
        else
        {
            throw e;
        }
    }
}


@Override
protected void onCreate( Bundle savedInstanceState )
{
    if(mWasError)
    {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(getApplicationContext(), mErrorMessage, Toast.LENGTH_SHORT).show();
                Looper.loop();

            }
        }.start();

        try
        {
            Thread.sleep(10000);
        }
        catch(InterruptedException e)
        {
        }
    }

    // Will crash here if there was an error
    super.onCreate(savedInstanceState);

      

+4


source


You can start loading libraries later when you show your main activity. If you have thrown an exception, you will be able to show Toast or AlertDialog.



Although this is all strange. Isn't the * .so part of the apk, you seem to know which one you have in your apk.

+1


source







All Articles