the world is a pixel
Posts tagged Java
Android screen height and width
Mar 12th
Context ctx = getContext(); Display display = ((WindowManager)ctx.getSystemService(ctx.WINDOW_SERVICE)).getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight();
Yes, there are easier ways to retrieve the screen width on Android but there are cases that this long code is the only solution. You may already have the Context. WindowManager or the Display and so it would be smaller, but this code is more general.
Java: invoking a method by name
Apr 29th

import java.lang.reflect.*; public class Foo { public void bar(int param){ System.out.println(param); } public static void main(String args[]){ Object f = new Foo(); try { Method m = f.getClass().getMethod("bar", int.class); m.invoke(f, 42); } catch (Exception e){ System.err.println(e); } } }
$ java Foo
42
calling commands in Java
Apr 8th
I don’t like the approach of calling native shell commands in any language instead of using multi platform libraries, but here is a little prof of concept Java program to call native commands.
import java.io.*; import java.util.*; public class Exec { public static void main(String args[]) throws IOException { Process proc = Runtime.getRuntime().e xec(args); BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } }
Usage:
java Exec VALID_COMMAND
Example:
$ java Exec echo hello
hello
ps: I had to write “e xec” instead of exec because it was triggering some very strange security protection in the blog engine here. If you need to compile this code change that. =P Also there’s no error handling, you should pass a valid command when executing this code.
Getting enviroment information on Android
Mar 16th
This is a simple program I wrote called Who Am I that shows informations about the device which it is running. Which can be useful for developers and maybe advanced users.

Download:
- WhoAmI.tar.bz2 – Eclipse project. It’s configured for Android platform 4 (1.6) but should work without problems in newer Android platform versions.
- WhoAmI.apk – Application installation Android package.
Main Activity source code:
package net.silveiraneto.whoami; import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.widget.EditText; public class WhoAmI extends Activity { private EditText mEditor; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.whoami); mEditor = (EditText) findViewById(R.id.editor); Object[][] properties = { {"Build.BOARD", Build.BOARD}, {"Build.BRAND", Build.BRAND}, {"Build.CPU_ABI", Build.CPU_ABI}, {"Build.DEVICE", Build.DEVICE}, {"Build.DISPLAY", Build.DISPLAY}, {"Build.FINGERPRINT", Build.FINGERPRINT}, {"Build.HOST", Build.HOST}, {"Build.ID", Build.ID}, {"Build.MANUFACTURER", Build.MANUFACTURER}, {"Build.MODEL", Build.MODEL}, {"Build.PRODUCT", Build.PRODUCT}, {"Build.TAGS", Build.TAGS}, {"Build.TIME", Build.TIME}, {"Build.USER", Build.USER}, }; for(Object[] prop: properties) { mEditor.append(String.format("%s: %s\n", prop[0], prop[1])); } } }
And its Android Manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.silveiraneto.whoami"> <application android:label="Who Am I"> <activity android:name="WhoAmI"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
The Caps Lock Java Socket Server
Feb 27th

Here is a simple server for those who are starting studying sockets or just needs a simple socket server example for reuse while writing your own behavior.
Features:
- A client should enter a string and the server would answer the same string, with each symbol in up case, when possible.
- Default port at 8080.
- One client at time.
- No multi threading. I said its a simple server.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { private static final int DEFAULT = 8080; public Server() { this(DEFAULT); } public Server(int port) { ServerSocket sock; try { sock = new ServerSocket(port); System.out.println(String.format("Listening on port %d.", port)); while (true) { try { Socket client = sock.accept(); System.out.println("A new connection was accepted."); BufferedReader in = new BufferedReader( new InputStreamReader(client.getInputStream())); OutputStreamWriter out = new OutputStreamWriter(client .getOutputStream()); String input = ""; while (!input.equals("exit")) { input = in.readLine(); if (input.equals("shutdown")) { System.exit(0); } out.write(input.toUpperCase() + "\r\n"); out.flush(); } in.close(); out.close(); client.close(); System.out.println("Connection closed."); } catch (NullPointerException npe) { System.out.println("Connection closed by client."); } } } catch (IOException ioe) { System.err.println(ioe); System.exit(-1); } } public static void main(String[] args) throws IOException { new Server(); } }
Usage:
$ javac Server.java
$ java Server
Listening on port 8080.
In another terminal:
$ telnet localhost 8080
Trying ::1…
Connected to localhost.
Escape character is ‘^]’.
hi
HI
The quick brown fox jumps over the lazy dog.
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
exi
EXI
exit
EXIT
Connection closed by foreign host.












