The following code snippets shows you how to get a Device ID of an Android phone. To be able to read the Device ID you need to update the AndroidManifest.xml file and add the READ_PHONE_STATE permission.
In this example we are going to create a simple Android application to print texts to a Bluetooth thermal printer. We’ll be using the Android library for ESC/POS Thermal Printer to develop this example.
We begin by creating an Android project with an Empty Activity. After the project is created we need to edit the app/build.gradle to add the required dependencies and the repository from which it will be downloaded.
Let’s now jump to the code snippet that will actually print our store receipt to the printer. The steps are quite simple.
After added the uses-permission in the AndroidManigest.xml we also need to check permission in the application, you’ll do it like this.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.BLUETOOTH}, MainActivity.PERMISSION_BLUETOOTH);
}
Open the connection to Bluetooth printer by calling the selectFirstPaired() method of the BluetoothPrintersConnections class. This will give us an instance of BluetoothConnection. If the connection is good we create an instance of EscPosPrinter by passing some parameters like the connection, printer dpi, width in millimeter and the printer’s number of character per line.
This example show you how to connect and read data from MySQL database directly from Android. The following steps and code snippet will show you how to do it.
Add the MySQL JDBC driver into your project dependencies. Open the app/build.gradle file and add the dependency.
If you want to connect to MariaDB you can change the JDBC driver dependency using 'org.mariadb.jdbc:mariadb-java-client:1.8.0', also update the JDBC url in the code snippet by replacing mysql with mariadb.
Next, add internet permission to our application in AndroidManifest.xml file.
Let’s connect, read data from the database and display the information on the screen. In the code snippet we create an AsyncTask to read the information from the database. In the doInBackground() method we open a connection to the database, create a PreparedStatement, execute a query, get a ResultSet and read the information from it. We pack the data into a Map and return it.
After the doInBackground() method finish its execution the onPostExecute() method will be called. In this method we take the result, the Map returned by the doInBackground() method, and set the values into the TextView components for display.
I need to create a Microsoft Word Mail Merge document in my Java Spring MVC application. But running it in Ubuntu server resulting in a document that missing the default font use in the document, which is the Calibri font. So I need to install the font in Ubuntu to make the document looks as expected.
Here what I need to do to install the font in my Ubuntu box. Starts by updating the repository package list to get latest packages information for upgrades or new package installation.
sudo apt-get update
Then install FontForge in our system. FontForge is a free and open source font editor, but in this case it will help to do the font conversion in the installation script on the upcoming step.
sudo apt-get install fontforge
Install the Microsoft Cabinet file un-packer. This is required for the next script to successfully install the fonts.
In this example you will learn how to create a generic class in Java. In some previous post in this blog you might have read how to use generic for working with Java collection API such as List, Set and Map. Now it is time to learn to create a simple generic class.
As an example in this post will create a class called GenericMachine and we can plug different type of engine into this machine that will be use by the machine to operate. For this demo we will create two engine type, a DieselEngine and a JetEngine. So let’s see how the classes are implemented in generic.
package org.kodejava.generics;
public class GenericMachine<T> {
private final T engine;
public GenericMachine(T engine) {
this.engine = engine;
}
public static void main(String[] args) {
// Creates a generic machine with diesel engine.
GenericMachine<DieselEngine> machine = new GenericMachine<>(new DieselEngine());
machine.start();
// Creates another generic machine with jet engine.
GenericMachine<JetEngine> anotherMachine = new GenericMachine<>(new JetEngine());
anotherMachine.start();
}
private void start() {
System.out.println("This machine running on: " + engine);
}
}
Now, for the two engine class we will only create an empty class so that the GenericMachine class can be compiled successfully. And here are the engine classes:
package org.kodejava.generics;
public class DieselEngine {
}
package org.kodejava.generics;
public class JetEngine {
}
The <T> in the class declaration tell that we want the GenericMachine class to have type parameter. We also use the T type parameter at the class constructor to pass the engine.