Tutorial by Examples: a

By using startActivityForResult(Intent intent, int requestCode) you can start another Activity and then receive a result from that Activity in the onActivityResult(int requestCode, int resultCode, Intent data) method. The result will be returned as an Intent. An intent can contain data via a Bundle ...
To create an IntentService, create a class which extends IntentService, and within it, a method which overrides onHandleIntent: package com.example.myapp; public class MyIntentService extends IntentService { @Override protected void onHandleIntent (Intent workIntent) { //Do so...
Starting a service is very easy, just call startService with an intent, from within an Activity: Intent intent = new Intent(this, MyService.class); //substitute MyService with the name of your service intent.putExtra(Intent.EXTRA_TEXT, "Some text"); //add any extra data to pass to the s...
Set up Android Studio Start by setting up Android Studio and then open it. Now, you're ready to make your first Android App! Note: this guide is based on Android Studio 2.2, but the process on other versions is mainly the same. Configure Your Project Basic Configuration You can start a new ...
As you use generic types with utility classes, you may often find that number types aren't very helpful when specified as the object types, as they aren't equal to their primitive counterparts. List<Integer> ints = new ArrayList<Integer>(); Java SE 7 List<Integer> ints = new A...
Let's say you want to define an interface that allows publishing / consuming data to and from different types of channels (e.g. AMQP, JMS, etc), but you want to be able to switch out the implementation details ... Let's define a basic IO interface that can be re-used across multiple implementations...
All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated. /** * Brief summary of this class...
All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated. /** * Brief summary of method, end...
A Pattern can be compiled with flags, if the regex is used as a literal String, use inline modifiers: Pattern pattern = Pattern.compile("foo.", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); pattern.matcher("FOO\n").matches(); // Is true. /* Had the regex not been compiled case...
Sometimes you may wish to read byte-input into a String. To do this you will need to find something that converts between byte and the "native Java" UTF-16 Codepoints used as char. That is done with a InputStreamReader. To speed the process up a bit, it's "usual" to allocate a b...
Opening with the default browser This example shows how you can open a URL programmatically in the built-in web browser rather than within your application. This allows your app to open up a webpage without the need to include the INTERNET permission in your manifest file. public void onBrowseCli...
All Javadoc comments begin with a block comment followed by an asterisk (/**) and end when the block comment does (*/). Optionally, each line can begin with arbitrary whitespace and a single asterisk; these are ignored when the documentation files are generated. /** * Fields can be documented as ...
You can get the value of other primitive data types as a String using one the String class's valueOf methods. For example: int i = 42; String string = String.valueOf(i); //string now equals "42”. This method is also overloaded for other datatypes, such as float, double, boolean, and ...
Executors accept a java.lang.Runnable which contains (potentially computationally or otherwise long-running or heavy) code to be run in another Thread. Usage would be: Executor exec = anExecutor; exec.execute(new Runnable() { @Override public void run() { //offloaded work, no need t...
A common Executor used is the ThreadPoolExecutor, which takes care of Thread handling. You can configure the minimal amount of Threads the executor always has to maintain when there's not much to do (it's called core size) and a maximal Thread size to which the Pool can grow, if there is more work t...
If your computation produces some return value which later is required, a simple Runnable task isn't sufficient. For such cases you can use ExecutorService.submit(Callable<T>) which returns a value after execution completes. The Service will return a Future which you can use to retrieve the r...
The ScheduledExecutorService class provides a methods for scheduling single or repeated tasks in a number of ways. The following code sample assume that pool has been declared and initialized as follows: ScheduledExecutorService pool = Executors.newScheduledThreadPool(2); In addition to the nor...
Java SE 5 It is possible to create package-level documentation in Javadocs using a file called package-info.java. This file must be formatted as below. Leading whitespace and asterisks optional, typically present in each line for formatting reason /** * Package documentation goes here; any docu...
Maps provide methods which let you access the keys, values, or key-value pairs of the map as collections. You can iterate through these collections. Given the following map for example: Map<String, Integer> repMap = new HashMap<>(); repMap.put("Jon Skeet", 927_654); repMap.p...
String str = "My String"; System.out.println(str.charAt(0)); // "M" System.out.println(str.charAt(1)); // "y" System.out.println(str.charAt(2)); // " " System.out.println(str.charAt(str.length-1)); // Last character "g" To get the nth charac...

Page 17 of 1099