AndroidChannel


Source link: https://github.com/skyfe79/AndroidChannel

AndroidChannel

AndroidChannel is helper library for inter thread communication between main thread and worker thread. AndroidChannel uses HandlerThread for inter thread communication.

Setup Gradle

dependencies {
  ...
  compile 'kr.pe.burt.android.lib:androidchannel:0.0.4' 
}

Example

AndroidChannel-ProgressLayout

I have reimplemented ProgressLayout by using AndroidChannel for example. You can check out souce code at AndroidChannel-ProgressLayout. It shows how to use AndroidChannel when you want to implement a custom animatable view.

Ping-Pong

You can make a channel between main thread and worker thread easily.

channel = new AndroidChannel(new AndroidChannel.UiCallback() {

  @Override
  public boolean handleUiMessage(Message msg) {

 if(msg.what == PING) {

 Log.d("TAG", "PING");

 channel.toWorker().sendEmptyMessageDelayed(PONG, 1000);

}

return false;
  
}
 
}
, new AndroidChannel.WorkerCallback() {

  @Override
  public boolean handleWorkerMessage(Message msg) {

 if(msg.what == PONG) {

 Log.d("TAG", "PONG");

 channel.toUI().sendEmptyMessageDelayed(PING, 1000);

}

return false;
  
}
 
}
);
 channel.toUI().sendEmptyMessage(PING);

Timer

If you use AndroidChannel, You can make Timer more easily. I already make Timer class for you. You can just use it. If you want to know how to implement Timer class, you just read souce code in the package.

timer = new Timer(1000, new Timer.OnTimer() {

  int count = 0;
  @Override
  public void onTime(Timer timer) {

count++;

textView.setText("count : " + count);

  
}
 
}
);
 timer.start();
 

You can also stop the timer like this.

@Override public boolean onTouchEvent(MotionEvent event) {

  if(event.getAction() == MotionEvent.ACTION_UP) {

if(timer.isAlive()) {

 timer.stop();

}
 else {

 timer.start();

}

  
}

  return super.onTouchEvent(event);
 
}

APIs

  • channel.open()
  • Use open() method to open channel. If you created a channel by Channel constructor, it is automatically open the channel by default.
  • channel.close()
  • Use close() method to close channel. close() method removes callbacks and messages in the message queue.
  • channel.toUI()
  • toUI() method returns main thread handler. If you want to send messages to ui thread you should use toUI() method.
  • channel.toWorker()
  • toWorker() method returns worker thread handler. If you want to send messages to worker thread you should use toWorker() method.

MIT License

The MIT License

Copyright © 2015 Sungcheol Kim, http://github.com/skyfe79/AndroidChannel

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Resources

Collection of source codes, utilities, templates and snippets for Android development.

AndroidResizer is a simple Java desktop app for automatically re-sizing android assets.

AR will downscale your assets and copy them into an android-style folder structure.

Android & Pure Java library to shape a voice with an instrument.

NaturalLenguageProcessor is an Android library that wraps text-processing.com API and gives you the possibility to stem, to tag and to analyze sentimentally the text you want.

Layout that contains a ViewPager and can slide vertically between 2 states (expanded and collapsed).

NodeFlow is an Android library that provides a simple way to visualize hierarchical content. Perfect for displaying items that are organized in categories / subcategories.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes