Category Archives: Android

「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;

}

}

「Android」response.getStatusLine().getStatusCode()の使い方

サンプルコード
public static InputStream get(String url)throws IOException ,Exception {

urlStr = url;
isConnecting = true;

HttpGet httpGet = new HttpGet(urlStr);
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,
connectionTimeout);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
return inputStream;}
else return null;

}
}

「Android入門」WebViewを利用するサンプルコード

1.activity_main.xml
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<WebView
android:id=”@+id/webView1″
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
</LinearLayout>

2、MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

view.loadUrl(url);
return true;
}
});
webView.loadUrl(“http://www.example.com”);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

3.AndroidManifest.xml
追記
<uses-permission android:name=”android.permission.INTERNET”/>

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