Monday, June 20, 2011

How get PDUs from SMS in Android

Today, I wanted to convert the "pdus" bytes back to hex, this is how to do it.

@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] aPDUArray = (Object[]) bundle.get("pdus");
String pduString = toHexString((byte[]) aPDUArray[0]);
}


private final static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };

public static String toHexString(byte[] array) {
int length = array.length;
char[] buf = new char[length * 2];

int bufIndex = 0;
for (int i = 0 ; i < length; i++)
{
byte b = array[i];
buf[bufIndex++] = HEX_DIGITS[(b >>> 4) & 0x0F];
buf[bufIndex++] = HEX_DIGITS[b & 0x0F];
}

return new String(buf);
}

Friday, June 10, 2011

isProcessRunning in Android - The hardway

public static boolean isProcessRunning(String paramString)
throws Exception
{
int i = 0;
Process localProcess = Runtime.getRuntime().exec("ps");
InputStream localInputStream = localProcess.getInputStream();
InputStreamReader localInputStreamReader = new InputStreamReader(localInputStream);
BufferedReader localBufferedReader = new BufferedReader(localInputStreamReader);
String str = localBufferedReader.readLine();
localBufferedReader.close();
localProcess.waitFor();

if (!str.contains(paramString))
break;

}
}