RSS

Tag Archives: java

Android: get active, current or foreground Activity


In our recent Android App development, we developed many static utility functions, which needs a Context as the argument:

public static void func1(Context context, …)
public static void func2(Context context, …)
public static void func3(Context context, …)

public static void funcN(Context context, …)

This is very cumbersome. We would like to refactor these utility functions into functions without the context argument/ A natural idea to pass in the application context at the very beginning, e.g. Application.onCreate() function, but not in all cases, can we use the application context. For instance, when you create a dialog or toast window, we need to use Activity as the context. So here comes the function that works:

image

Visit this gist to get the source code.

 
Leave a comment

Posted by on July 13, 2016 in Android, Programming

 

Tags: , , , , , , ,

Google Book Search in C#: A step by step walk-through tutorial


Several years earlier, I wrote a python script to perform ISBN book search: given the isbn number, the python script simply dumps the books title, publisher and author information, which are then used to rename the pdf that is downloaded into a more informational file name:

123456789a.pdf –> Elsevier_2010_A simple and effective geometric representation for irregular porous structure modeling.pdf

It is a handy script but unfortunately I lost it after a hard disk failure!

And then I decide to recover this from my brain, but I would like to implement it in another my favorite language, C#.

image

Source code of this project can be downloaded from GitHub here.

Read the rest of this entry »

 
14 Comments

Posted by on December 18, 2014 in Dotnet/C#, Java, Programming

 

Tags: , , , , , , , , , , , ,

Apple Swift: top 5 familiar code patterns for java or C# coders


Apple just released its new Swift programming languages, and java and C# coders may be happy about this as there are many features that you will feel familiar with:

  • Type inference and var keyword:

var str =”Some String”
var iValue = 32

  • Nullable (Nil-able)

int? a=3;

  • Comments and block comments

// Single line comment
/* This
   is
  block comment
*/

  • Ternary Conditional Operators

boolean AreYouHappy = TodayIsWeekend? true: false;

  • String format (String Interpolation): the C# {0} equivalent in String.Format():

int money = 123456
string money_str = “We have \(money) dollars”  // We have 123456 dollars

See online documentation here.

 
Leave a comment

Posted by on June 21, 2014 in Programming

 

Tags: , , , , , , , , , ,

Java Socket handshaking: the simplest example


In an earlier post, I have discussed how to implement socket communication with python. I recently was asked to implement similar functions, but in Java. Here is how I did that.

  • Create a new Java project, let’s name it JSocketClient;
  • Create another Java project, let’s name it JSocketServer;
  • In both projects, create an App class, and add a static Main function respectively:

    image

Read the rest of this entry »

 
Leave a comment

Posted by on August 20, 2013 in Java, Programming

 

Tags: , , , ,