| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

差分

ナビゲーションに移動 検索に移動
575 バイト追加 、 2020年2月15日 (土) 08:00
編集の要約なし
==Android Tips==
[[Android][Java][Eclipse]]
==概要==
==開発環境==
===GAE===
[[http://typea.info/blg/glob/2010/08/android_gae_windows7.html Android (実機) と GAE を連携させるためのデバッグ環境を Windows7 に構築する]]
===SDK ソースコード===
=====[CentOS 初期設定] [Gitのインストール(Linux)]=====
===画面の向きを取得する===
<&lt;blockquote>&gt;API Level 8 以降は getRotation () を使うこと 0:portrait, 1:landscape<&lt;/blockquote>&gt;
Display display = getWindowManager().getDefaultDisplay();
*AndroidManifest.xml に指定
*タイトルバーを非表示、フルスクリーン
<&lt;activity : android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >&gt;
===最大化表示(ステータスバーを隠す)===
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
<&lt;blockquote>&gt;逆の場合、add と clear を変える<&lt;/blockquote>&gt;
=====XML=====
*AndroidManifest.xml に指定
*タイトルバーを非表示、フルスクリーン
<&lt;activity : android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >&gt;
===UIについて===
txtHoge.setTextColor(getResources().getColor(R.color.lightgrey));
<&lt;blockquote>&gt;txtHoge.setTextColor(R.color.lightgrey) とするのは、色の値として、リソースIDが渡されるためNG<&lt;/blockquote>&gt;
===EditText に最初にフォーカスがあたり、IMEが自動起動してしまうのを抑制===
===同じアプリケーションのアクティビティを呼び出す===
=====マニフェストファイルの application セクションの中に以下を記述=====
<&lt;activity android:name=".OtherActivity"/>&gt;
=====インテントを利用して呼び出し=====
Intent intent = new Intent(getApplicationContext(),
             OtherActivity.class);
startActivity(intent);
<&lt;blockquote>&gt;データの受け渡しには、Intent.putExtra を利用<&lt;/blockquote>&gt;
=====Activity.onCreate =====
Spinner spFontSize = (Spinner) findViewById(R.id.spn_font_size);
ArrayAdapter<&lt;CharSequence> &gt; spFontSizeAdapter
= ArrayAdapter.createFromResource(this,
R.array.ary_font_size,
spFontSize.setAdapter(spFontSizeAdapter);
===[http://typea.info/blg/glob/2010/09/androidx06ht-desire-activity-activity.html Activity の状態を一時的に保存する]===
[[http://typea.info/blg/glob/2010/09/androidx06ht-desire-activity-activity.html Activity の状態を一時的に保存する]]
===[http://android.siprop.org/index.php?%CA%D9%B6%AF%B2%F1%2FAndroid%20SDK%20WG%20%C2%E81%B2%F3%20%A5%BB%A5%C3%A5%B7%A5%E7%A5%F3%A1%CA2008.10.25%A1%CB コンフィグレーション]===
*デバイスのコンフィグレーションが変更されたら、UIはそのコンフィグレーションにマッチするように更新する必要があるため、Activitiは再スタートされる
===[http://typea.info/blg/glob/2010/09/android-arrayadapter.html ArrayAdapter に ラジオボタンを置く]===
[[http://typea.info/blg/glob/2010/09/android-arrayadapter.html ArrayAdapter に ラジオボタンを置く]]
===TabActivity(タブによるアクティビティ切り替え) サンプル===
[[http://typea.info/blg/glob/2010/11/android-tabactivity.html TabActivity]]
===ListActivity(リスト表示画面)サンプル===
[[Android スケルトン ListActivity] [ListActivity]]
===PreferenceActivity(設定画面の作成) サンプル===
[[http://typea.info/blg/glob/2010/10/android-android-hacks.html PreferenceActivity]]
===透明なActivityを作成する===
*http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android
====res/values/styles.xml に以下の内容を記述====
<&lt;?xml version="1.0" encoding="utf-8"?>&gt; <&lt;resources>&gt; <&lt;style name="Theme.Transparent" parent="android:Theme">&gt; <&lt;item name="android:windowIsTranslucent">&gt;true<&lt;/item>&gt; <&lt;item name="android:windowBackground">&gt;@android:color/transparent<&lt;/item>&gt; <&lt;item name="android:windowContentOverlay">&gt;@null<&lt;/item>&gt; <&lt;item name="android:windowNoTitle">&gt;true<&lt;/item>&gt; <&lt;item name="android:windowIsFloating">&gt;true<&lt;/item>&gt; <&lt;item name="android:backgroundDimEnabled">&gt;false<&lt;/item>&gt; <&lt;/style>&gt; <&lt;/resources>&gt;
====AndroidManifest.xml のActivityのtheme属性にに以下の記述====
<&lt;activity android:name=".EditScoreActivity" android:theme="@style/Theme.Transparent"><&gt;&lt;/activity>&gt;
===Activity をダイアログとして表示する===
*@android:style/Theme.Dialog を利用
<&lt;activity android:name=".EditScoreActivity" android:theme="@android:style/Theme.Dialog"><&gt;&lt;/activity>&gt;
==Service==
===[Android Service] [Service]===
===ListView の分離線を変更する===
====XMLから====
<&lt;ListView android:id="@+id/android:list"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:divider="#D7D7D7"
android:dividerHeight="1px">&gt; <&lt;/ListView>&gt;
====コードから====
*ListViewのIDは、以下の様にする。
*TextViewは、リストがない場合
<&lt;ListView android:id="@android:id/list" ・・・><&gt;&lt;/ListView>&gt; <&lt;TextView android:id="@android:id/empty" ・・・><&gt;&lt;/TextView>&gt;
または、
<&lt;ListView android:id="@+id/android:list" ・・・><&gt;&lt;/ListView>&gt; <&lt;TextView android:id="@+id/android:empty" ・・・><&gt;&lt;/TextView>&gt;
===ListViewの背景色の設定をしても、スクロールすると黒くなってしまう===
*[http://typea.info/blg/glob/2012/02/android-8.html ListViewのスクロールバーを大きくする]
===ListViewのコンテキストメニュー選択時にアイテムを取り出す===
[[http://typea.info/blg/glob/2012/02/android-8.html ListViewのコンテキストメニュー選択時にアイテムを取り出す]]
===カスタムViewを利用する===
*http://developer.android.com/guide/topics/ui/custom-components.html
<&lt;view xmlns:android="http://schemas.android.com/apk/res/android"
class="info.typea.iromegane.ColorTile"
android:id="@+id/col_tile" ><&gt;&lt;/view>&gt;
===カスタムViewを作成しレイアウトはXMLで行う===
}
===ページめくり===
[[http://typea.info/blg/glob/2010/07/androidx06ht_desire_2.html ページをめくるサンプル]]
===Spinnerの使い方例===
=====string.xml=====
<&lt;string-array name="ary_font_size">&gt; <&lt;item>&gt;70<&lt;/item>&gt; <&lt;item>&gt;80<&lt;/item>&gt; <&lt;item>&gt;90<&lt;/item>&gt; <&lt;item>&gt;100<&lt;/item>&gt; <&lt;/string-array>&gt;
=====Activity=====
// フォントサイズ
spFontSize = (Spinner) findViewById(R.id.spn_font_size);
ArrayAdapter<&lt;CharSequence> &gt; spFontSizeAdapter
= ArrayAdapter.createFromResource(this,
R.array.ary_font_size,
spFontSize.setPrompt(getResources().getText(R.string.lbl_font_size));
for (int i=0; i<&lt;spFontSize.getCount(); i++) {
int fs = Integer.parseInt(spFontSizeAdapter.getItem(i).toString());
if (hogehoge.getFontSize() == fs) {
===スクロールさせる===
*ScrollViewを利用
<&lt;?xml version="1.0" encoding="utf-8"?>&gt; <&lt;LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">&gt;
<&lt;ScrollView android:id="@+id/scrl_word_editor"
android:layout_height="wrap_content"
android:layout_width="match_parent">&gt;
<&lt;LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">&gt;
<&lt;TextView><&gt;&lt;/TextView>&gt; <&lt;EditText><&gt;&lt;/EditText>&gt;
:
<&lt;/LinearLayout>&gt; <&lt;/ScrollView>&gt; <&lt;/LinearLayout>&gt;
==インテント==
===暗黙的 Intent で Twitter や Evernote と連携する===
===テキストエディタ等にファイルの処理をさせる===
=====setDataAndType を使って、ファイルのURIとMIMEタイプを同時に渡す=====
<&lt;blockquote>&gt;個別に渡すと後に渡した方しか有効にならない(バグ?仕様 Android2.1 X06HT)<&lt;/blockquote>&gt;
File f = new File(path);
=====AndroidManifest.xml=====
*android:launchMode="singleTop" を指定。指定しないと別のActivityが生成されてしまう。
<&lt;activity android:name=".TimeKeeperActivity" android:launchMode="singleTop">&gt;
:
=====インテントを設定し、通知を行う=====
*formatted="false" を指定する
=====string.xml例=====
<&lt;string name="msg_location_msg" formatted="false">&gt;経度 %9.6f、緯度 %9.6f 付近のメッセージ<&lt;/string>&gt;
=====コード例=====
**エミュレータを使用する
**コマンドプロンプトのフォントおよび文字コードを変更する
C:\Users\piroto>&gt;adb shell
# cd /data/data/info.typea.eitangoroid.pro/databases
cd /data/data/info.typea.eitangoroid.pro/databases
txtPaint.setAntiAlias(true);
FontMetrics fontMetrics = null;
for (int i=5; i<&lt;40 ; i++) {
txtPaint.setTextSize(i);
fontMetrics = txtPaint.getFontMetrics();
if ((fontMetrics.bottom - fontMetrics.top) > &gt; baseHeight ) {
i--;
txtPaint.setTextSize(i);
===TraceView===
*トレース開始位置に以下を記述
<&lt;blockquote>&gt;SDカードがマウントされていること<&lt;/blockquote>&gt;
Debug.startMethodTracing("test");
*トレース終了位置に
Debug.stopMethodTracing();
*トレースが完了したら、トレースファイルを取得
C:\work>&gt;adb pull /sdcard/test.trace
*TraceView を実行
C:\work>&gt;cd C:\Programs\android-sdk-windows\tools C:\Programs\android-sdk-windows\tools>&gt;traceview c:\work\test.trace
[[File:0163_traceview01.jpg]]
===スクリーンキャプチャ===
===カメラアプリで、FileNotFoundException===
*http://www.bpsinc.jp/blog/archives/date/2010/05/08
<&lt;blockquote>&gt;カメラアプリで、FileNotFoundException /sys/android_camera2/htcwc が発生したら、端末の再起動を行う。<&lt;/blockquote>&gt;
===SDKソースコードのステップ実行===
=====パーミッション=====
<&lt;uses-permission android:name="android.permission.VIBRATE"><&gt;&lt;/uses-permission>&gt;
camera.setParameters(p);
<&lt;blockquote>&gt;Froyo 化してからは、上記方法が使えなくなった(むしろ使えていたのがおかしかった!?)ため、以下の対応<&lt;/blockquote>&gt;
====[http://typea.info/blg/glob/2010/10/android-x06ht-desire-2.html カメラプレビューが横向きに表示されてしまう!の対応]====
=====必要なパーミッション=====
<&lt;uses-permission android:name="android.permission.WAKE_LOCK"/>&gt; <&lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>&gt;
==Web==
===Httpクライアントタイムアウトを設定する===
[[http://typea.info/blg/glob/2010/09/android-http.html Httpクライアントタイムアウトを設定する]]
HttpPost HttpGet = new HttpGet(uri);
HttpParams httpParms = new BasicHttpParams();
*[http://hc.apache.org/downloads.cgi httpcore]
*[http://hc.apache.org/downloads.cgi httpmime]
<&lt;blockquote>&gt;httpmime は、httpcomponents-client に httpclient とともにアーカイブされている。<&lt;/blockquote>&gt;
=====サンプル=====
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpPost post = new HttpPost("http://hogehoge.com/hoge");
List<&lt;NameValuePair> &gt; parms = new ArrayList<&lt;NameValuePair>&gt;();
parms.add(new BasicNameValuePair("lat", this.lat));
parms.add(new BasicNameValuePair("lng", this.lng));
HttpPost post = new HttpPost("http://hogehoge.com/hoge");
List<&lt;NameValuePair> &gt; parms = new ArrayList<&lt;NameValuePair>&gt;();
parms.add(new BasicNameValuePair("lat", String.valueOf(loc.getLatitude())));
parms.add(new BasicNameValuePair("lon", String.valueOf(loc.getLongitude())));
===Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead ===
Android requires compiler compliance level 5.0 or 6.0. Found '1.4' instead. Please use Android Tools > &gt; Fix Project Properties.
*プロジェクトのプロパティから、Java Compiler
*Compiler compliance level を 1.6 等 適切なものに
*Android Tools > &gt; Fix Project Properties
*リビルド

案内メニュー