SoundPressureLevel: Crashes if lost focus

I use SoundPressureLevel, extension version 20190317, in one of my Android App Inventor apps. However, if the screen containing the SoundPressureLevel component is not focused (i.e. switching app or opening another screen), the entire app crashes. As a workaround, I tried to disable the component before switching screens, and it worked, though the problem of switching apps still remains unsolved. Are there any workarounds or fixes for that?

Try this

com.KIO4_VUmeterA11.aix

http://kio4.com/appinventor/239C_extensiones_nivel_microfono.htm

It kinda works, but the app lags when switching between screens, which the app didn't. Was that caused by this extension?

what extension are you referring to SoundPressureLevel or com.KIO4_VUmeterA11.aix?

com.KIO4_VUmeterA11.aix

Use "Virtual Screen" with VerticalArrangement.

I finally grabbed the crash log via adb: (Using SoundPressureLevel)

--------- beginning of crash
05-27 05:51:29.950  7869  7869 E AndroidRuntime: FATAL EXCEPTION: main
05-27 05:51:29.950  7869  7869 E AndroidRuntime: Process: appinventor.ai_REDACTED.REDACTED_SPL1, PID: 7869
05-27 05:51:29.950  7869  7869 E AndroidRuntime: java.lang.RuntimeException: Unable to stop activity {appinventor.ai_REDACTED.REDACTED_SPL1/appinventor.ai_REDACTED.REDACTED_SPL1.Screen1}: java.lang.UnsupportedOperationException
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:5178)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:5150)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.ActivityThread.handleStopActivity(ActivityThread.java:5215)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:43)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:106)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.os.Looper.loopOnce(Looper.java:201)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:288)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:7872)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: Caused by: java.lang.UnsupportedOperationException
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at java.lang.Thread.suspend(Thread.java:1282)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at com.rit.appinventor.components.runtime.SoundPressureLevel.onStop(SoundPressureLevel.java:161)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at com.google.appinventor.components.runtime.Form.onStop(Form.java:770)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1584)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.Activity.performStop(Activity.java:8527)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:5170)
05-27 05:51:29.950  7869  7869 E AndroidRuntime: 	... 14 more

These are the relevant code:

@Override /*public class SoundPressureLevel extends AndroidNonvisibleComponent implements OnStopListener, OnResumeListener, Deleteable, OnInitializeListener */
public void onStop() {
    if (isEnabled) {
        stopListening(); /* Stops a AudioRecord object's recording process */
        if (!threadSuspended) {
            Log.d(LOG_TAG, "spl suspend thread");
            threadSuspended = true;
            soundChecker.suspend(); /* soundChecker is a Thread */
        }
    }
}

Oh, I found the reason: Java Thread Primitive Deprecation

In the article, it says both stop() and suspend() (which the extension uses) won't function and will always raise and error (java.lang.UnsupportedOperationException). The extension's code will have to be modified in order to work.

Thank you for your advice, but Virtual Screen is not a choice for me. The app is an assignment from my teacher, and one of the requirements is to properly handle the screens.

There are a couple of activity life cycle extensions which monitor when the app is paused (e.g. when you switch to another app) and allow an action/event to be taken at that point.

I made a fork of SoundPressureLevel which solves the problem: Release 20230527 · Emojigit/SoundPressureLevel · GitHub

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.