Development

Apps can’t miss in your Android – RemoteControl for Earphones

I’m starting a new section called “Apps can’t miss in your Android“. This section will have reviews and tips for useful applications, that will help to extract all potentical from your Android.

I’ll start with RemoteControl for Earphones. It just cancel a vantage of iPhone over Android: to control media players using the earphones’s button.

The button’s behavior is to pause and start music. RemoteControl for Earphones allow you runs almost all commands of a media player. To play or pause, press button once, to skip forward, press button twice, to skip rewind, press four times…

RemoteControl for Earphones

App screen (Android Market)

This application is for free and don’t have advertisement.

Most players are incompatible, but it work very well in Morotola Droid’s player, Songbird, but not work in Winamp and players of Samsung Galaxy S and Motorola Atrix.

Link:  RemoteControl for Earphones
Price: Free
Advertisement: No


How to read and write CLOB fields

The Character Large Object (or CLOB) is a commonly found in databases and used to store high quantity of characters.

At MySQL, for example, this field is called MEMO.

Writing CLOB field

The method setAsciiStream of PreparedStatement allow to pass data to a CLOB.

ps.setAsciiStream(bindPosition, inputStream, textLength);

bindPosition – Position in PreparedStatment’s CLOB field.
inputStream – Used to pass data.
textLength – Data (text) length.

Full code:

String sql = "INSERT INTO TABLE (text) VALUES(?)";
		try{
			String txt = readTxtFile();
			ByteArrayInputStream bais = new ByteArrayInputStream(txt.getBytes());

			PreparedStatement ps = conexao.prepareStatement(sql);
			//CLOB is '?' at first position
			ps.setAsciiStream(1, bais, txt.length());

			ps.execute();

			ps.close();
		}catch (Exception e) {
			e.printStackTrace();
		}

Retrieving CLOB data

We’re using SELECT clause, this clause returns a ResultSet to fetch data and, calling getClob method, passing column name or your position in the query, it give to you a Clob object.

rs.getClob("xml");

Full code:

String sql = "SELECT xml FROM TEST";
		try{
			PreparedStatement ps = conexao.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();

			while(rs.next()){
				Clob clob = rs.getClob("xml");
				BufferedReader reader = new BufferedReader(clob.getCharacterStream());
				StringBuffer strBuf = new StringBuffer();

				String linha = null;
				while((linha = reader.readLine()) != null){
					strBuf.append(linha);
//Character.LINE_SEPARATOR insert break line
					strBuf.append((char)Character.LINE_SEPARATOR);
				}

				System.out.println("=========== CLOB ===========");
				System.out.println(strBuf.toString());
			}

			rs.close();
			ps.close();
		}catch (Exception e) {
			e.printStackTrace();
		}

That’s it! Easy and painless ;)

Download full code here

To write a BLOB see this tutorial.


Installing .apk packages at Android SDK emulator

Sometimes our applications depends of third-part applications for tests or to access resources of emulator that aren’t available by default.

In directories of SDK, commonly in platform-tools, there is an executable called adb, with it we’ll to install third-part applications.

You will need an application (.apk). Browse internet or copy from your Android smartphone using applications like AppSaver.

- Run the emulator, you can use SDK Manager or Eclipse plugins.

- Using DOS Command (Windows) or terminal (Linux), open directory platform-tools and run commando line below:

$ adb install <.apk path>

This message will be displayed

$ adb install Application.apk
125 KB/s (1091937 bytes in 8.474s)
        pkg: /data/local/tmp/Application.apk
Success

Now, the third-part application is available at emulator menu, like have installed in your smartphone.

Agora o aplicativo estará disponível no menu do emulador. ;)

 


Setting up internet access through proxy server

Today I had a trouble in my workplace when a client application, running through a terminal (DOS prompt), not reach the server hosted in the internet, this is because the company where I work use proxy.

After some tries I found two solutions to solve my access problem: first was pass as Java parameters the proxy’s configurations.

$ java -Dhttp.proxyHost=serv -Dhttp.proxyPort=port -Dhttp.proxyUser=user -Dhttp.proxyPassword=pass  ClassJava

-Dhttp.proxyHost = IP or host name of server proxy
-Dhttp.proxyPort = Proxy port
-Dhttp.proxyUser = User
-Dhttp.proxyPassword = Password

This way the class ClassJava will have access to internet through proxy server.

Another way, was insert the settings in own class:

public static void main(String[] args){
...
System.getProperties().put("proxySet", "true");
System.getProperties().put("http.proxyHost", "serv");
System.getProperties().put("http.proxyPort", "port");
System.getProperties().put("http.proxyUser", "user");
System.getProperties().put("http.proxyPassword", "password");
...
}

proxySet = Connect, or not, through proxy server
http.proxyHost = IP or host name of server proxy
http.proxyPortt = Proxy port
http.proxyUse = User
http.proxyPassword = Password

Using this resource you can set your application to access the internet through proxy server.

I hope help you.  Bye! ;)


Creating transparent and shaped windows using Java Swing/AWT

The Java AWT offer various possibilities about window manipulation. Today I’ll talk about two possibilities using class AWTUtilities.

Building a transparent window

To make a Window (JFrame, JDialog …) with alpha effect, you should invoke the method AWTUtilities.setWindowOpacity. This method parameters are: the window to apply transparency and transparency degree, that can be between 0 (zero) and 1, being 0 invisible and 1 totally visible.

JFrame  window = new JFrame("My Window");

//70% of transparency
AWTUtilities.setWindowOpacity(window, .7f);
window.setSize(800,600);
window.setVisible(true);

The result will be:

Changing window shape

To change the window shape you should use the method AWTUtilities.setWindowShape. This method parameters are: window that will be changed and the new shape (java.awt.Shape) of the window.

The most efficiently form to use is to implement the method componentResized(), cause you may recompute the window and components size

Here I’ll use a triangle with 70% of transparency.

final JFrame  window = new JFrame("My Window");

try {
	//Add the ComponentListener to implement componentResized method
	window.addComponentListener(new ComponentAdapter(){
		@Override
		//building componentResized
		public void componentResized(ComponentEvent e) {
			int[] x = {0,400,800}; //Pontos X do polígono
			int[] y = {600,0,600}; //Pontos Y do polígono

			//Triangle with 800 w x 600 h
			Shape shape = new Polygon(x, y, 3);

			AWTUtilities.setWindowShape(window, shape);

			//70% of transparency
			AWTUtilities.setWindowOpacity(window, 0.7f);
		}
	});
} catch (SecurityException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} catch (IllegalArgumentException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

window.setUndecorated(true); //removing title bar
window.setSize(800,600);
window.setVisible(true);

Notice that the method setUndecorated( ) was invoked with true as value. This method are responsible by hide the title bar (one with icon and the maximize, minimize and close buttons). This is really necessary, because with title bar visible the window are not be shaped.

The result:

I hope you enjoy… ;)


  • AdSense

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