Android – Transfering data between apps and computer over USB cable

There are many Android apps where you can run some tasks without wireless connection (4G, 3G, Wi-fi or Bluetooth), only using USB cable. Below, I’ll describe an easy way to do it.

As usual, you’ll need Android Development Kit, that can be found here.

At client development, we need to change ADB ports, used by socket connections. We can do this by running the following command:

<PLATFORM-TOOLS DIR>/adb forward tcp:59900 tcp:59900

Using Java:

Runtime.getRuntime().exec("<PLATFORM-TOOLS DIR>/adb forward tcp:59900 tcp:59900");

Remember that “<PLATFORM-TOOLS DIR>” is the path to “platform-tools” directory, located in Android SDK installation directory.

We only changed the “ears” of ADB to listen the port 59900, used in our connection.

Lets send a “Hello World!”:

Socket socket = new Socket("", 59900);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write("Hello World!".getBytes());

Now the Client is ready, lets go to the application.

Add the permissions “android.permission.ACCESS_NETWORK_STATE” and “android.permission.INTERNET” to the AndroidManifest.xml.

Given the appropriate permissions, we do application to listen the requests coming from port 59900, using the class ServerSocket from package java.net:

ServerSocket server = new ServerSocket(59900);
Socket socket = server.accept();

With Socket object, we can get the data using getInputStream():

InputStreamReader reader = new InputStreamReader(socket.getInputStream());
BufferedReader br = new BufferedReader(reader);
msg = br.readLine();

We are working with Strings, therefore I used the class BufferedReafer, to get entire line with method readLine();

It’s done! Now we can communicate application and computer using only USB cable.

It’s not allowed using main thread to opening socket, so, you’ll need to open the socket in a separated Thread or to extend the class AsyncTask.

If you still have doubts about this method, I’ll make available the example used in this tutorial here.

How to show any font in your site with Google Fonts and Cufon

Unfortunately, to display texts using not defaults fonts, this fonts must be installed in user’s computer. What is a problem to the designers.

Now I show two ways to solve that, Google Web Fonts and Cufon.

Google Web Fonts

This tool, unfortunately, have a limited number of fonts that can be used, but your rendering is faster than Cufon.

How to use:

  • First, you must choose the fonts for your site. Go to http://www.google.com/webfonts.
  • Click “Add to Collection” to choose them. In this tutorial I’ve selected “Eater“, “Give You Glory” and “Bonbon“. Memorize their names to later.
  • In the blue bar below, click “Use“. Will be displayed tips for fonts and some methods to attach in your site (“3. Add this code to your website“). I’ve selected the tab “@import”:

  • Copy text box content and past in <style type=”text/css”>…</style> tag:
<style type="text/css">
@import url(http://fonts.googleapis.com/css?family=Eater|Give+You+Glory|Bonbon);
  • Use their names in “font-family” normally:
    <p style="font-family:Eater">Fonte Eater</p>
    <p style="font-family:Give You Glory">Fonte Give You Glory</p>
    <p style="font-family:Bonbon">Fonte Bonbon</p>

Google Web Fonts is ready to use!


Has long been used, inclusive some WordPress templates. It allows use any fonts, an advantage against Google Web Fonts. Uploading the font, it generates scripts to be used in your site. At current version (1.09), Cufon uses Canvas, taking more processing time and some browsers doesn’t support it.

How to:

  • Download .js file. Open URL http://cufon.shoqolate.com/generate/, right click “Download” and “Save As…” (or “Salve Link As…“).
  • Continuing at http://cufon.shoqolate.com/generate/, upload your font filling “Regular typeface” field. If you want bold and/or italic, upload respective files in “Bold typeface“, “Italic typeface” and “Bold italic typeface“, not mandatory.
  • Check “The EULAs of these fonts allow Web Embedding” (basically informs that you allowed to use these fonts), configure how font will be used in your site setting configuration fields and read terms and check “I acknowledge and accept these terms“, if agree.
  • Click “Let’s do this” to download another .js file, referencing fonts uploaded.
  • Attach javascripts (.js files) in HTML file:
<script type="text/javascript" language="JavaScript" src="<PATH>/cufon-yui.js"></script>
<script type="text/javascript" language="JavaScript" src="<PATH>/<FONT>.js"></script>
  • Let’s put Cufon to work! Call “replace” method of “Cufon” object, setting tag name…:
<script type="text/javascript" language="JavaScript">
//Replaces fonts of tags H1
  • Or id attribute:
<script type="text/javascript" language="JavaScript">
//Replaces font of tags with id="header"
  • Or CSS class
<script type="text/javascript" language="JavaScript">
//Replaces font of tags with class="highlight"
  • You can set more fonts, setting attribute “fontFamily” in method “replace“, this way:
<script type="text/javascript" language="JavaScript">
Cufon.replace('h1', {fontFamily: 'Font 1'});
Cufon.replace('#tagID', {fontFamily: 'Font 2});
Cufon.replace('.class', {fontFamily: 'Font 3});

Cufon is ready!

Click here to download this tutorial.

Now, there is no limit to fonts!

PL/SQL – Retrieving current milliseconds

The function to_char with sysdate is the most known way to retrieve current time in PL/SQL. The way:

select to_char(sysdate,'dd/MM/yyyy HH24:mi:ss') date from dual;

You get this:

14/08/2012 13:24:32

But, if need get milliseconds also, we should to use systimestamp and the characters ‘FFn‘, where ‘n’ will be number of digits. The milliseconds are the goal, then ’4′ is the number.

select to_char(systimestamp,'dd/MM/yyyy HH24:mi:ss.FF4') date from dual;

And results:

14/08/2012 13:24:33.2110

If you wants only milliseconds, jus keep ‘FF4’ and remove the others characters:

select to_char(systimestamp,'FF4') millis from dual;

Now you will get:


Good coding!

Oracle – Escaping character ‘_’ in LIKE clause

How should you know, underline(‘_’), in LIKE, is considered a wildcard that means “any character”. Query will replace underline by any other character where ‘_’ is.

Following table bellow:

|  WORD   |
| BEE     |
| SEE     |
| FEE     |
| SHE     |
| _EE     |

And running

select column from word where column LIKE '_EE';


| BEE     |
| SEE     |
| FEE     |
| _EE     |

Besides “_EE“, expected initially, “BEE“, “SEE” and “FEE” has been returned.

We have to use escape after LIKE to include ‘_’ at the search. Escape needs a character to transform it in a wildcard that ignores the next one, thus, the new wildcard should be added before ‘_’, like this.

select column from word where column LIKE '\_EE' escape '\';

Now, the result will be:

| _EE     |

I hope this helps some one else! 🙂

Android – Transferring files between emulator and the computer

Here is  a very useful tutorial for who needs to copy files between Android emulator and your PC.

There are some ways to transfer file from PC to emulator and from emulator to PC. I will show up two ways.

Using Eclipse plugin – ADT

Runs Android’s emulator. Click in the button “Opens the Android Virtual Device Manager” at Eclipse’s menu.

Select your device and click “Start”.

Wait until the emulator boot.

Back to Eclipse, opens “File Explorer” view clicking in Window > Show View, Other…

Select Android > File Explorer.

The tab “File Explorer” will show up.

Copying files from PC to emulator

Select the directory where you want to put the file and click button “Push a file onto the device” ().

Select the file and wait for the end of process.

Copying files from emulator to PC

Be sure the emulator still running, in the tab “File Explorer”, select files that you want to copy to PC and click buton “Pull a file from device” ().

Choose where to put the file.

Using MS-DOS Prompt (Windows Command Line)

We’ll not use Eclipse IDE, only the tool available in Android SDK and MS-DOS Prompt.

Open up “Android Virtual Device Manager” running “AVD Manager.exe”, available in Android SDK instalation directory.

Choose your device in the lista and click “Start”.

Wait until the emulator boot and open MS-DOS Prompt.

Type command:

<ANDROID-SDK-DIRECTORY>/platform-tools/adb devices
Replace “<ANDROID-SDK-DIRECTORY>” by path of installation of Android SDK.

A list, with attached devices, will show up:

List of devices attache
emulator-5554   device

Connect the ADB to emulator using the command

<ANDROID-SDK-DIRECTORY>/platform-tools/adb connect localhost:5554
The number “5554” is the same displayed  at description “emulator-5554” and at emulator’s title bar .

The line “connected to localhost:5554” will be displayed.

Copying files from PC to emulator

To send file located in directory of your PC to emulator, all you need to do is type

<ANDROID-SDK-DIRECTORY>/platform-tools/adb push text.txt /sdcard
You can replace the argument “/sdcard” by internal path where you want to put the file in emulator

Wait util copy end and done! The file was copied to emulator.

Copying files from emulator to a directory in your PC

If you wants to get a file from and put it in your PC, type the command bellow:

<ANDROID-SDK-DIRECTORY>/platform-tools/adbadb pull /sdcard/text.txt <DESTINY-PATH>
Replace “<DESTINY-PATH>” by complete path where you want store the file

Done! The file was copied again to your PC.

  • Advertisement

  • Advertisement

  • Copyright © 1996-2010 André L. S.. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress