Using The Android Sensors PRO

The android device has sensors we can use in our code. We can get data coming from these resources using the SensorManager object. The following code sample shows how to do it. package com.abelski.samples; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import […]

Java 7 Type Inference for Generic Instance Creation PRO

As of Java 7 we can take out the type arguments required to instantiate a generic class as long as the compiler can infer the type arguments from the context. Many developers name the remaining pair of angle brackets as ‘diamond’. package com.abelski.samples; import java.util.LinkedList; import java.util.List; public class GenericsTypeInference { public static void main(String[] […]

Java 7 Multiple Exception Types Catch PRO

As with Java 7 we can handle more than one type of exception using a single catch block. We should use the ‘|’ operator for putting them together in the same catch statement. When catch handles more than one type of exception the catch parameter is implicitly final. We won’t be able to assign it with […]

The Java 7 try-with-resources Statement PRO

As of Java 7 we can code a try statement that declares one or more resources. Each resource is an object that must be closed after the program is finished. The try-with-resources statement ensures that each resource is closed at the end of the statement. Each object considered as a resource must be instantiated from a class […]

Java 7 Binary Number System PRO

Java 7 allows us to express integral numbers using the binary numbers system. We just need to prefix our integral number with either ‘ob’ or ‘0B’. package com.abelski.samples; public class BinaryDemo { public static void main(String[] args) { int a = 0b101; int b = 0b011; int c = a | b; System.out.println(c); } } […]

Java 7 Switch Case PRO

As of Java 7 we can write a switch case statement that works on strings. This new capability might assist us with writing a shorter code. package com.abelski.samples; public class Demo { public static void main(String[] args) { String operator = args[1]; double numA = Double.parseDouble(args[0]); double numB = Double.parseDouble(args[2]); String result; switch(operator) { case […]

Java 7 Underscores in Numeric Literals PRO

As of Java 7, we can improve the readability of our code by adding underscores in between digits in numerical literals. package com.abelski.samples; public class SeparatorClass { public static void main(String[] args) { double num = 1_424_234.532; System.out.println(num); } } The following video clips overviews this code sample and shows its execution.

Pausing & Resuming Java Script Execution in Web View PRO

When executing code written in Java Script in our web view we would like to pause it when the user moves to another activity and resumes it when he returns. The following code sample includes two activities. The first activity includes a web view that executes code written in Java Script. The second activity is […]

The sscanf Function in PHP PRO

This function allows us to extract values from a string that applies a specific pattern. Each place holder (e.g. %s) refers a specific value in our string. Each value that matches a specific place holder is assigned to a variable. We should pass over our variable to be assigned by the values that match the […]

XML DOM Parsing in Java PRO

JAXP (Java API for XML) provides us with the capability to parse XML documents using a DOM parser. The following code sample shows a simple parsing using the JAXP DOM parser. package com.abelski.samples; import java.io.IOException; import java.net.URL; import java.io.InputStream; import java.net.HttpURLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; […]

Update cookies preferences