Skip to content

Instantly share code, notes, and snippets.

@brandhill
brandhill / gist:42a884a3057c14cf454a
Last active August 29, 2015 14:03
Android - Getting the IP address of client or getting the information(SSID) of Clients connected to Hotspot
public void getClientList() {
int macCount = 0;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] splitted = line.split(" +");
#################
## Eclipse
#################
*.pydevproject
.project
.metadata
bin/
tmp/
*.tmp
@brandhill
brandhill / gist:9c947a3e2881dff66bd3
Last active August 29, 2015 14:04
android : Convert dp to pixel / Convert pixel to dp
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
@brandhill
brandhill / gist:c899222c25b9f4ece755
Last active August 29, 2015 14:04
android : get an uri of an image resource
public static final String ANDROID_RESOURCE = "android.resource://";
public static final String FORESLASH = "/";
public static Uri resIdToUri(Context context, int resId) {
return Uri.parse(Consts.ANDROID_RESOURCE + context.getPackageName()
+ Consts.FORESLASH + resId);
}
public class URLUtils {
private static final String fGOOGLE_DOC_URL = "http://docs.google.com/gview?embedded=true&url=%s";
private enum GoogleDocType{
PDF(".pdf"), DOC(".doc"), XLS(".xls"), DOCX(".docx"), XLSX(".xlsx");
private String mSuffix;
GoogleDocType(String suffix){
mSuffix = suffix;
@brandhill
brandhill / gist:f57ded3c5db2a69e734c
Created August 11, 2014 06:36
android - 得知目前的 heap memory 使用情況。
Log.i(TAG, "userdMemory: " + Debug.getNativeHeapSize() / 1048576);
@brandhill
brandhill / gist:666f8d64371ade54ddd8
Last active August 29, 2015 14:05
android - avoid clipboard manager crash (only) on android 2.x
//Clipboard API has changed on level 11 of Android SDK. Here is some code to handle both versions from arinkverma.
// get text
@SuppressWarnings("deprecation")
public void putText(String text){
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES. HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(text);
@brandhill
brandhill / gist:3e59f8bdca5050e6ec5c
Created August 18, 2014 06:01
Merging two arrayLists into a new arrayList, with no duplicates (Java)
// 方法 1
ArrayList al = new ArrayList();
// add elements to al, including duplicates
HashSet hs = new HashSet();
hs.addAll(al);
al.clear();
al.addAll(hs);
/**
* Copyright 2013 Bo Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
/**
* Copyright 2013 Bo Wang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software