android-weak-handler | Memory safer implementation of android.os.Handler

 by   badoo Java Updated: 8 months ago - 1.2 License: MIT

Download this library from

Build Applications

kandi X-RAY | android-weak-handler REVIEW AND RATINGS

Memory safer implementation of android.os.Handler.

kandi-support
Support

  • android-weak-handler has a medium active ecosystem.
  • It has 1499 star(s) with 288 fork(s).
  • It had no major release in the last 12 months.
  • On average issues are closed in 36 days.
  • It has a neutral sentiment in the developer community.

quality kandi
Quality

  • android-weak-handler has 2 bugs (0 blocker, 0 critical, 0 major, 2 minor) and 15 code smells.

security
Security

  • android-weak-handler has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
  • android-weak-handler code analysis shows 0 unresolved vulnerabilities.
  • There are 0 security hotspots that need review.

license
License

  • android-weak-handler is licensed under the MIT License. This license is Permissive.
  • Permissive licenses have the least restrictions, and you can use them in most projects.

build
Reuse

  • android-weak-handler releases are available to install and integrate.
  • Deployable package is available in Maven.
  • Build file is available. You can build the component from source.
  • android-weak-handler saves you 197 person hours of effort in developing the same functionality from scratch.
  • It has 485 lines of code, 54 functions and 4 files with 0 % test coverage
  • It has medium code complexity. Code complexity directly impacts maintainability of the code.
Top functions reviewed by kandi - BETA

kandi has reviewed android-weak-handler and discovered the below as its top functions. This is intended to give you an instant insight into android-weak-handler implemented functionality, and help decide if they suit your requirements.

  • Adds a runnable to the chain .
  • Remove a runnable callable .
  • Send a message delayed to the Executor .
  • Get the current thread s executor .
  • Send an empty message .
  • Return true if any of the specified object has a value .
  • Send an empty message at time .
  • Queues a Runnable to execute .
  • Send an empty message delayed with a delay
  • Post a runnable to the front of the queue .

android-weak-handler Key Features

Memory safer implementation of android.os.Handler

android-weak-handler examples and code snippets

  • Usage
  • What's wrong with 7 android?

Usage

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.badoo:android-weak-handler:1.2'
}

What's wrong with 7 android?

 void startServer() {
            mStarted = true;
            mWakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);

            new Thread(() -> {
                try {
                    byte[] data = new byte[8 * 1024];//bucketful with fix size
                    InetAddress address = BnjUtils.getLocalIpAddress();

                    DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
                    mServerSocket = new DatagramSocket(0, address);

                    final InetAddress localAddress = ((InetSocketAddress) mServerSocket.getLocalSocketAddress()).getAddress();
                    final int port = mServerSocket.getLocalPort();
                    mListener.onServerCreated(localAddress, port);

                    while (mStarted) {
                        try {

                            mServerSocket.receive(datagramPacket); // blocks until something is received
                            /*
                             * Send bytes to app module.
                             */
                            byte[] receiveData = new byte[datagramPacket.getLength()];

                            System.arraycopy(datagramPacket.getData(), datagramPacket.getOffset(), receiveData, 0, datagramPacket.getLength());
                            mListener.onReceive(receiveData, datagramPacket.getAddress());

                        } catch (IOException e) {
                            mListener.onServerStartFailed(e);
                        }
                    }
                } catch (SocketException e) {
                    Log.e(mTag, "Socket Exception");
                } finally {
                    if (mServerSocket != null && !mServerSocket.isClosed()) {
                        mServerSocket.close();
                    }
                }
            }).start();
        }

COMMUNITY DISCUSSIONS

Top Trending Discussions on android-weak-handler
  • What's wrong with 7 android?
Top Trending Discussions on android-weak-handler

QUESTION

What's wrong with 7 android?

Asked 2018-Mar-22 at 20:20

I have Service for receive udp datagramm sockets

     private void startServer(final UdpServerListener listener) {

     mStarted = true;
            mWakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);
            Looper myLooper = Looper.myLooper();

            new HandlerThread("TcpServerThread") {
                @Override
                protected void onLooperPrepared() {
                    new Handler(getLooper()).post(() -> {
                        try {
                            byte[] data = new byte[8 * 1024];

                            InetAddress address = BnjUtils.getLocalIpAddress();

                            DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
                            mServerSocket = new DatagramSocket(0, address);

                            final InetAddress localAddress = ((InetSocketAddress) mServerSocket.getLocalSocketAddress()).getAddress();
                            final int port = mServerSocket.getLocalPort();


                            while (mStarted) {

                                try {

                                   mServerSocket.receive(datagramPacket); // blocks until something is received
                                    /*
                                     * Send bytes to app module.
                                     */
                                    byte[] receiveData = new byte[datagramPacket.getLength()];

                                    System.arraycopy(datagramPacket.getData(), datagramPacket.getOffset(), receiveData, 0, datagramPacket.getLength());

     //POST MY DATA TO BINDER VIA HANDLE LOOPER AND LISTENER                                       
    new Handler(myLooper).post(() -> listener.onReceive(receiveData,datagramPacket.getAddress()));


                                } catch (IOException e) {
                                    new Handler(myLooper).post(() -> listener.onServerStartFailed(e));
                                }
                            }
                        } catch (SocketException e) {
                            Log.e(mTag, "Socket Exception");
                        } finally {
                            if (mServerSocket != null && !mServerSocket.isClosed()) {
                                mServerSocket.close();
                            }
                            getLooper().quitSafely();
                        }
                    });
                }
            }.start();
        }

So result data i post to listener via Binder

  //POST MY DATA TO BINDER VIA HANDLE LOOPER AND LISTENER       
 new Handler(myLooper).post(() -> listener.onReceive(receiveData,datagramPacket.getAddress()));

See my listener:

@NonNull
private UdpServerService.UdpServerListener getUdpServerServiceListener() {

    if (mUdpServerServiceListener == null) {
        mUdpServerServiceListener = new UdpServerService.UdpServerListener() {

            @Override
            public void onReceive(final byte[] bytes, final InetAddress inetAddress) {
                if (mSocketListener != null && mListenerLooper != null) {

                    //TRY TO RECEIVW MY DATA HERE
                }
            }

        };
    }
    return mUdpServerServiceListener;
}

See my service connection class with binder

private class MyServiceConnection implements ServiceConnection {

        UdpServerService.UdpServerBinder binder;

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            if (service instanceof UdpServerService.UdpServerBinder && mServerState == SERVER_STARTED) {
                binder = (UdpServerService.UdpServerBinder) service;
                binder.setListener(getUdpServerServiceListener());
                binder.startServer();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //do something on disconnect
        }

So, it all works greate but in old devices with version 5 and 6 Android.

But in my GalaxyS7, or in My Asus Zenfone 3

with Android 7 application freezes

and does not respond user touch until it is not stopped.

Maybe triggered some limiting mechanism, but I do not know anything about it.

maybe wrong build.gradle see it

    apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

apply plugin: 'idea'

android {
    compileSdkVersion 26

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        useLibrary 'org.apache.http.legacy'
    }

    buildTypes {

        debug {
            minifyEnabled false
            debuggable true
            signingConfig signingConfigs.debug
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        release {
            minifyEnabled true
            debuggable false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

    }
    packagingOptions{
        exclude 'META-INF/rxjava.properties'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //gson parser
    implementation 'com.google.code.gson:gson:2.8.0'
    //kotlin
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    // rxjava
    implementation  'io.reactivex:rxjava:1.1.6'
    implementation  'io.reactivex:rxandroid:1.2.1'
    implementation  'com.artemzin.rxjava:proguard-rules:1.1.0.0'

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.8'

    //help to avoid  memory leaks
    implementation 'com.badoo.mobile:android-weak-handler:1.0'

    //guava for Android:
    implementation 'com.google.guava:guava:23.6-android'

}

kotlin {
    experimental {
        coroutines "enable"
    }
}

ANSWER

Answered 2018-Jan-08 at 17:43

My solution is make this code simple, just work receive data in simple Thread, and post its data in the same thread:

 void startServer() {
            mStarted = true;
            mWakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);

            new Thread(() -> {
                try {
                    byte[] data = new byte[8 * 1024];//bucketful with fix size
                    InetAddress address = BnjUtils.getLocalIpAddress();

                    DatagramPacket datagramPacket = new DatagramPacket(data, data.length);
                    mServerSocket = new DatagramSocket(0, address);

                    final InetAddress localAddress = ((InetSocketAddress) mServerSocket.getLocalSocketAddress()).getAddress();
                    final int port = mServerSocket.getLocalPort();
                    mListener.onServerCreated(localAddress, port);

                    while (mStarted) {
                        try {

                            mServerSocket.receive(datagramPacket); // blocks until something is received
                            /*
                             * Send bytes to app module.
                             */
                            byte[] receiveData = new byte[datagramPacket.getLength()];

                            System.arraycopy(datagramPacket.getData(), datagramPacket.getOffset(), receiveData, 0, datagramPacket.getLength());
                            mListener.onReceive(receiveData, datagramPacket.getAddress());

                        } catch (IOException e) {
                            mListener.onServerStartFailed(e);
                        }
                    }
                } catch (SocketException e) {
                    Log.e(mTag, "Socket Exception");
                } finally {
                    if (mServerSocket != null && !mServerSocket.isClosed()) {
                        mServerSocket.close();
                    }
                }
            }).start();
        }

So now, works without freeze.

Source https://stackoverflow.com/questions/48151842

Community Discussions, Code Snippets contain sources that include Stack Exchange Network

VULNERABILITIES

No vulnerabilities reported

INSTALL android-weak-handler

You can use android-weak-handler like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the android-weak-handler component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .

SUPPORT

For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .

Implement android-weak-handler faster with kandi.

  • Use the support, quality, security, license, reuse scores and reviewed functions to confirm the fit for your project.
  • Use the, Q & A, Installation and Support guides to implement faster.

Discover Millions of Libraries and
Pre-built Use Cases on kandi