Listen for event dispatches in component through the extension

I would like to listen for EventDispatches in a particular Component from an extension.
For example, when Timer in the Clock component as Timer. Is it possible to know when an event is dispatched?

Thanks.

1 Like

even i need that!

1 Like

See this example from @pavi2410

Possible to do it dynamicaly without a predefined callback?

1 Like

Probably not possible.

I am stuck here, I am extending the Form class.

public class ModifiedForm extends Form {

        @Override
        public boolean canDispatchEvent(Component component, String str) {
            return true;
        }
        
        @Override
        public boolean dispatchEvent(Component component, String str, String str2, Object[] objArr) {
            Log.e("EventDispatchedListener", str);
            return true;
        }

        @Override
        public void dispatchGenericEvent(Component component, String str, boolean z, Object[] objArr) {
            Log.e("EventDispatchedListener", str);
        }
    }

This should work fine:

public class FForm extends Form{
        @Override
        public boolean canDispatchEvent(Component component, String str) {
            return true;
        }

        @Override
        public boolean dispatchEvent(Component component, String str, String str2, Object[] objArr) {
            getDispatchDelegate().dispatchEvent(component,str,str1,objArr);
            Log.e("EventDispatchedListener", str);
            return true;
        }

        @Override
        public void dispatchGenericEvent(Component component, String str, boolean z, Object[] objArr) {
            getDispatchDelegate().dispatchGenericEvent(component,str,z,objArr);
            Log.e("EventDispatchedListener", str);
        }
    }

However, how are you going to replace form?

Thanks for the code, I do not know how I should replace them. I just created an object like this:

 ModifiedForm myForm = new ModifiedForm();

and expectedly, nothing happened.

Actually using this method you can easily create components in background.

public void CreateComponent(String componentName){
        try {
            Class<?> cClass = Class.forName("com.google.appinventor.components.runtime."+componentName);
            Component component = (Component) cClass.getConstructor(new Class[]{ComponentContainer.class}).newInstance(new CComponentContainer());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public class FForm extends Form{

        @Override
        public boolean canDispatchEvent(Component component, String str) {
            return true;
        }

        @Override
        public boolean dispatchEvent(Component component, String str, String str2, Object[] objArr) {
            getDispatchDelegate().dispatchEvent(component,str,str2,objArr);
            Log.e("EventDispatchedListener", str);
            return true;
        }

        @Override
        public void dispatchGenericEvent(Component component, String str, boolean z, Object[] objArr) {
            getDispatchDelegate().dispatchGenericEvent(component,str,z,objArr);
            Log.e("EventDispatchedListener", str);
        }

    }
    public class CComponentContainer implements ComponentContainer {
        public FForm form;
        public CComponentContainer(){
            form = new FForm();
        }

        @Override
        public Activity $context() {
            return form.$context();
        }

        @Override
        public Form $form() {
            return form;
        }

        @Override
        public void $add(AndroidViewComponent androidViewComponent) {
        }

        @Override
        public void setChildWidth(AndroidViewComponent androidViewComponent, int i) {
        }

        @Override
        public void setChildHeight(AndroidViewComponent androidViewComponent, int i) {
        }

        @Override
        public int Width() {
            return 0;
        }

        @Override
        public int Height() {
            return 0;
        }
    }
1 Like

That's not a problem. I did everylast thing. The last remaining thing of the extension is events. :hugs:

I replaced the normal component container with the class above, but nothing happened.

ps: I think it will only work if the component calls form.dispatchevent

Try from a service.

Yes I am trying it from the service, CComponentContainer and FForm is public-sub-class

You mean component was not created?

03-16 18:13:51.877  1222  1222 E BACKGROUND: starttask
03-16 18:13:51.878  1222  1222 E BACKGROUND: ID clock
03-16 18:13:51.878  1222  1222 E BACKGROUND: 1
03-16 18:13:51.878  1222  1222 E BACKGROUND: [[clock, Timer, 3232]]
03-16 18:13:51.879  1222  1222 E BACKGROUND: END

According to logs from the application, the component was created and a method named Timer() was invoked for testing.

So what's the conclusion?

The event is not logged and event dispatched does nothing.

1 Like

@ewpatton sir kindly have a look.

I did test it again, the component object is always null and produces and error with crash.

1 Like

pls explain me i cant understand the above codes

1 Like