Category Archives: Android

「Android入門」InputMethodManagerでソフトキーボードを消す方法

サンプルコード
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
imm.hideSoftInputFromWindow(view, InputMethodManager.HIDE_NOT_ALWAYS);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

「Android入門」root化されているかどうかを確認する方法」

Javaコード
public class RootUtil {
public static boolean isDeviceRooted() {
return chkRootMethod1() || chkRootMethod2() || chkRootMethod3();
}

private static boolean chkRootMethod1() {
String buildTags = android.os.Build.TAGS;
return buildTags != null && buildTags.contains(“test-keys”);
}

private static boolean chkRootMethod2() {
String[] paths = { “/system/app/Superuser.apk”, “/sbin/su”, “/system/bin/su”, “/system/xbin/su”, “/data/local/xbin/su”, “/data/local/bin/su”, “/system/sd/xbin/su”,
“/system/bin/failsafe/su”, “/data/local/su”, “/su/bin/su”};
for (String path : paths) {
if (new File(path).exists()) return true;
}
return false;
}

private static boolean chkRootMethod3() {
Process process = null;
try {
process = Runtime.getRuntime().exec(new String[] { “/system/xbin/which”, “su” });
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
if (in.readLine() != null) return true;
return false;
} catch (Throwable t) {
return false;
} finally {
if (process != null) process.destroy();
}
}
}

「Android」重複した要素をリストから排除するコード

方法1
public static void removeDuplement(List emLst) {
for ( int i = 0 ; i < emLst.size() – 1 ; i ++ ) {
for ( int j = emLst.size() – 1 ; j > i; j — ) {
if (emLst.get(j).equals(emLst.get(i))) {
emLst.remove(j);
}
}
}
System.out.println(emLst);
}

方法2
public static void removeDupElement(List emLst) {
HashSet h = new HashSet(emLst);
emLst.clear();
emLst.addAll(h);
System.out.println(emLst);
}

方法3
public static void removeDupElementWithOrder(List emLst) {
Set set = new HashSet();
List newLst = new ArrayList();
for (Iterator iter = emLst.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newLst.add(element);
}
emLst.clear();
emLst.addAll(newLst);
System.out.println( ” remove duplicate ” + emlst);
}

「Android」Viewの内容をBitmapの保存方法

Javaコード:
public Bitmap createViewBitmap(View v) {
Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}

「Android入門」プロセスIDを取得するコード

サンプルコード
ActivityManager mActivityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> mRunningProcess = mActivityManager.getRunningAppProcesses();

int i = 1;
for (ActivityManager.RunningAppProcessInfo amProcess : mRunningProcess) {
Log.e(“homer Application”, (i++) + ”  PID = ” + amProcess.pid + “; processName = ” + amProcess.processName);
}

「Android」IPアドレスを取得するサンプルコード

サンプルコード:
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}

「Android」ブラウザを開くサンプルコード

サンプルコード
Intent viewIntent = new
Intent(“android.intent.action.VIEW”,Uri.parse(“http://xxx.sample.com”));

startActivity(viewIntent);

「Android」サーバー側にsocket通信をするサンプルコード

1.サーバーに接続
socket = new Socket(“172.17.2.100”,54321);

2.Socketを取得
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);

3.socketの読み書き
out.println(message);

4.socketのクローズ
out.close();

「Android」doPost()でJSON文字列を取得する方法

javaコード
public static String doPost(List<NameValueSample> params, String url)
throws Exception {
String result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
if (params != null) {

HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
httpPost.setEntity(entity);
}

HttpResponse httpResp = httpClient.execute(httpPost);
if (httpResp.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResp.getEntity(), “UTF-8”);
} else {
Log.i(“HttpPost”, “HttpPost method request failure”);
}

return result;
}

「Android」クライアント側にpostリクエストを実装するコード

/**
* サーバーにリクエスト
*
* @param model
* @param paramList
* @return
*/
private static JSONObject doPost(int model, List<NameValuePair> paramList) {

HttpPost httpPost = new HttpPost(INNER_URL);
HttpEntity entity = null;
try {
entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
httpPost.setEntity(entity);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}

HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse != null
&& httpResponse.getStatusLine().getStatusCode() == 200) {
String element = EntityUtils.toString(httpResponse.getEntity(),
HTTP.UTF_8);
if (element.startsWith(“{“)) {
try {
return new JSONObject(element);
} catch (JSONException e) {
e.printStackTrace();
}
}
}

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return null;

}

}

1 / 2012345...1020...最後 »