ライブラリ化
業務アプリでは、多数の機能があり、デバッグ時は単体で実行できるように機能毎にプロジェクトを作成したいし、製品時ではライブラリ化して組み込む。
・プロジェクト→プロパティ→Android→ライブラリーにある「Is Library」にチェックをつける。
・メインとなるプロジェクト(メニュー)のマニフェストに
<activity android:name="クラス名"/>
を追加する
・インテントで起動する
Intent intent = new Intent(); intent.setClassName(getPackageName(), "クラス名"); startActivity(intent);
あと、ライブラリ化するとSubVersionの管理フォルダをコピーしようとして警告ができるのでウィンドウ→設定→Java→コンパイラー→ビルドの「フィルターするリソース」を「*.launch, .svn」にする
ソフトウェアキーボードを最初から表示させる
EditTextがある場合にActivity開始時にソフトウェアキーボードを表示したいが下記のようにしても表示されない。
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
下記のように遅延させれば表示できる。
final EditText editText1 = (EditText)this.findViewById(R.id.editText1); Handler handler = new Handler(); Message m = Message.obtain(handler, new Runnable() { public void run() { InputMethodManager manager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); manager.showSoftInput(editText1, InputMethodManager.SHOW_FORCED); } } ); handler.sendMessageDelayed(m, 200);
SJISで書かれたテキストファイルを1行づつ読み込む
BufferedReader fi = new BufferedReader(new InputStreamReader(new FileInputStream("ファイル名"), "MS932")); try { while ((String lineData = fi.readLine()) != null){ // 1行毎の読み込み } finally { fi.close(); }
例外エラーの呼び出しスタックをLogCatに表示
} catch (Exception e) { StringWriter s = new StringWriter(); PrintWriter prt = new PrintWriter(s); e.printStackTrace(prt); Log.d(TAG, s.toString()); }
自動ペアリング
Bluetooth携帯プリンタに印字しようとしたら、どうもAndroidではBluetooth機器をペアリングしないとRFCOMMやSPPでは接続できないみたい。
一応、BluetoothDevice.javaを見てみるとcreateRfcommSocketメソッドでのBluetoothSocketクラス生成時にauthフラグが固定でtrueになっているから、これがfalseで設定できればあるいはと思ったけど・・・
FHTPR231ではペアリングがそもそもできないようなので、BLM80で印字してみた。結果印字できた。
ただ、毎回ペアリングを手動で行うのはめんどうなので自動でペアリングする方法
// 自動ペアリング処理 protected boolean execPairing(String bluetoothAddress, String pinCode) throws Exception { // Bluetoothアダプタ取得 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); // BluetoothデバイスをMACアドレスから取得 BluetoothDevice device = adapter.getRemoteDevice(bluetoothAddress); // ペアリング開始処理呼び出し Method createBond = device.getClass().getMethod("createBond", new Class[] {}); Boolean result = (Boolean)createBond.invoke(device); if (!result.booleanValue()) { return false; } // PINコードをUTF8に変換 Method convertPinToBytes = BluetoothDevice.class.getMethod("convertPinToBytes", new Class[] { String.class }); byte[] pinCodes = (byte[])convertPinToBytes.invoke(BluetoothDevice.class, pinCode); // PINコード登録 Method setPin = device.getClass().getMethod("setPin", new Class[] { byte[].class }); result = (Boolean)setPin.invoke(device, pinCodes); if (!result.booleanValue()) { return false; } return true; }
本当はこの後にBroadcastReceiverでACTION_BOND_STATE_CHANGEDをキャッチする処理が
いると思うけど・・・
しかし、なんかリフレクションでしか呼べないメソッドばっかりだなぁ。
実機でデバッグを行う
HTC Desire HDの場合は下記サイトよりHTC Syncをダウンロードしてセットアップ
http://www.htc.com/uk/SupportDownload.aspx?p_id=324&cat=3&dl_id=1073
>adb devices
と行うと下記のように表示される
List of devices attached HT0CRRX04089 device
のでEclipseで実行→実行の構成でターゲットを手操作に切り替える