Skip to content

wrong constructor for android.content.ServiceConnection #1107

Closed

Description

@NickIliev

From @farfromrefug on July 5, 2018 8:24

In the android17.d.ts the constructor for android.content.ServiceConnection is wanting an implementation parameter which is wrong.
All implementation parameters for android constructor should be removed.
This is quite important as your tic won't work because of that and if you put a null parameter you app will crash.

Copied from original issue: NativeScript/NativeScript#6031

Activity

  1. vtrifonov commented on Jul 5, 2018

    @vtrifonov
    Contributor

    @farfromrefug in the android typings we generate constructor with a given implementation for every interface as you can see from the comment above:

    /**
     * Constructs a new instance of the android.content.ServiceConnection interface with the provided implementation.
     */

    android.content.ServiceConnection is actually an interface, so it doesn't make sense to have a constructor for it with no parameters as you cannot instantiate it. What we do is to give you a way to provide implementation using which we will create a class implementing the given interface.

  2. farfromrefug commented on Jul 6, 2018

    @farfromrefug
    Contributor

    @vtrifonov I didn't know this was possible. However the constructor without implementation still exist and works. I use it all the time. The definition (dts generator) files should also contain (generate) them..

  3. vtrifonov commented on Jul 6, 2018

    @vtrifonov
    Contributor

    @farfromrefug can you please provide an example how you use this?
    Thanks in advance!

  4. farfromrefug commented on Jul 6, 2018

    @farfromrefug
    Contributor
    function initServiceConnection() {
        if (ServiceConnection) {
            return;
        }
    
        class ServiceConnectionImpl extends android.content.ServiceConnection {
            constructor(private owner: WeakRef<BgServiceProvider>) {
                super();
                return global.__native(this);
            }
    
            onServiceDisconnected(name: android.content.ComponentName) {
                this.owner.get().unbindService();
            }
    
            onServiceConnected(name: android.content.ComponentName, binder: android.os.IBinder) {
                this.owner.get().handleBinder(binder);
            }
        }
    
        ServiceConnection = ServiceConnectionImpl as any;
    }

    and to create the object

     initServiceConnection();
            this.serviceConnection = new ServiceConnection(new WeakRef(this));

    this is the way listeners and such are used in almost every NativeScript module I have seen

  5. added this to the 4.2.0 milestone on Jul 13, 2018
  6. vtrifonov commented on Jul 13, 2018

    @vtrifonov
    Contributor

    Thank you for the example. We've added the parameterless constructors in the android d.ts generator tool and in the tns-platform-declarations package with that PR - NativeScript/NativeScript#6044

  7. farfromrefug commented on Jul 13, 2018

    @farfromrefug
    Contributor

    @vtrifonov that's so cool! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions