Adding a Time Delay in an Extension

I'm currently new to extension development. I started with the basic gradient extension, it is working fine. Now I want to add a delay in the execution. So when the function is called, it change the gradient of the component 10 seconds later (I know it's weird to add such a delay but I'm learning and experimenting and stuff...).
I'm using AppyBuilder Code Editor and here is my code:

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.AndroidViewComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.View;
import java.util.concurrent.TimeUnit;

@DesignerComponent(version = 1,
                    category = ComponentCategory.EXTENSION,
                    description = "",
                    nonVisible = true,
                    iconName = "")

@SimpleObject(external = true)

public class Gradient extends AndroidNonvisibleComponent {
    
    public Gradient(ComponentContainer container) {
        super(container.$form());
    }
    
    @SimpleFunction(description = "")
    public void SetGradientBackground(AndroidViewComponent component, Object orientation, int color1, int color2) {
    View view = component.getView();
    GradientDrawable gradientDrawable = new GradientDrawable(
            (GradientDrawable.Orientation) orientation,
            new int[] {color1, color2});

    // Setting the gradientDrawable as the background of view after 10 seconds.
    TimeUnit.SECONDS.sleep(10);
    view.setBackground(gradientDrawable);
    }
    @SimpleProperty(description = "")
    public Object BottomRightToTopLeft() {
    return GradientDrawable.Orientation.BR_TL;
    }
    }

And this is the error log

Buildfile: /projects/goldv2/appinventor-sources/appinventor/build.xml

extensions:

clean:
   [delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/build

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:34: The following error    [delete] Deleting directory /projects/goldv2/appinventor-sources/appinventor/components/reports

init:

common_CommonUtils:

init:

CommonUtils:

common_CommonVersion:

init:

CommonVersion:
     [exec] Result: 128
     [exec] Result: 128

CopyToRunLibDir:

components_AndroidRuntime:

init:
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/build/components
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports/raw
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/reports/html

CommonConstants:
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
    [javac] Compiling 6 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] 1 warning
    [javac] Creating empty /projects/goldv2/appinventor-sources/appinventor/components/build/classes/CommonConstants/com/google/appinventor/components/common/package-info.class
      [jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants.jar
      [jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/build/components/CommonConstants-gwt.jar

HtmlEntities:
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
    [javac] Compiling 1 source file to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/HtmlEntities
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] 1 warning
      [jar] Building jar: /projects/goldv2/appinventor-sources/appinventor/components/build/HtmlEntities.jar

common_CommonVersion:

init:

CommonVersion:
     [exec] Result: 128
     [exec] Result: 128

AndroidRuntime:
    [mkdir] Created dir: /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
    [javac] Compiling 332 source files to /projects/goldv2/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
    [javac] /projects/goldv2/appinventor-sources/appinventor/components/src/com/appybuilder/kaustubhrakhade/Gradient/Gradient.java:54: error: unreported exception InterruptedException; must be caught or declared to be thrown
    [javac]     TimeUnit.SECONDS.sleep(10);
    [javac]                           ^
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 error
    [javac] 1 warning

BUILD FAILED
/projects/goldv2/appinventor-sources/appinventor/build.xml:35: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:372: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/components/build.xml:141: The following error occurred while executing this line:
/projects/goldv2/appinventor-sources/appinventor/build-common.xml:118: Compile failed; see the compiler error output for details.

Total time: 4 seconds

I'm not understanding where am I going wrong?

What about searching error on Google?

1 Like

Basically I want to know how to handle this

Ok, thank you...problem solved.
From next time I'll also search on Google and not just in the community...


replaced Thread.sleep(10000);
with

try
    {
        Thread.sleep(10000);
    }
    catch(InterruptedException ex)
    {
        Thread.currentThread().interrupt();
    }
1 Like

Dangerous ground, time delays....
Make sure that this will work without other blocks racing away after your delay has started.

1 Like

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