Created
November 16, 2010 00:03
-
-
Save rummelonp/701214 to your computer and use it in GitHub Desktop.
Androidアプリをハードウェアキー対応するサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mituliii.hardwarekeysupportsample; | |
public enum HKKeyCodeMap | |
{ | |
KEYCODE_UNKNOWN ("UNKNOWN", 0), | |
KEYCODE_SOFT_LEFT ("SOFT_LEFT", 1), | |
KEYCODE_SOFT_RIGHT ("SOFT_RIGHT", 2), | |
KEYCODE_HOME ("HOME", 3), | |
KEYCODE_BACK ("BACK", 4), | |
KEYCODE_CALL ("CALL", 5), | |
KEYCODE_ENDCALL ("ENDCALL", 6), | |
KEYCODE_0 ("0", 7), | |
KEYCODE_1 ("1", 8), | |
KEYCODE_2 ("2", 9), | |
KEYCODE_3 ("3", 10), | |
KEYCODE_4 ("4", 11), | |
KEYCODE_5 ("5", 12), | |
KEYCODE_6 ("6", 13), | |
KEYCODE_7 ("7", 14), | |
KEYCODE_8 ("8", 15), | |
KEYCODE_9 ("9", 16), | |
KEYCODE_STAR ("STAR", 17), | |
KEYCODE_POUND ("POUND", 18), | |
KEYCODE_DPAD_UP ("DPAD_UP", 19), | |
KEYCODE_DPAD_DOWN ("DPAD_DOWN", 20), | |
KEYCODE_DPAD_LEFT ("DPAD_LEFT", 21), | |
KEYCODE_DPAD_RIGHT ("DPAD_RIGHT", 22), | |
KEYCODE_DPAD_CENTER ("DPAD_CENTER", 23), | |
KEYCODE_VOLUME_UP ("VOLUME_UP", 24), | |
KEYCODE_VOLUME_DOWN ("VOLUME_DOWN", 25), | |
KEYCODE_POWER ("POWER", 26), | |
KEYCODE_CAMERA ("CAMERA", 27), | |
KEYCODE_CLEAR ("CLEAR", 28), | |
KEYCODE_A ("A", 29), | |
KEYCODE_B ("B", 30), | |
KEYCODE_C ("C", 31), | |
KEYCODE_D ("D", 32), | |
KEYCODE_E ("E", 33), | |
KEYCODE_F ("F", 34), | |
KEYCODE_G ("G", 35), | |
KEYCODE_H ("H", 36), | |
KEYCODE_I ("I", 37), | |
KEYCODE_J ("J", 38), | |
KEYCODE_K ("K", 39), | |
KEYCODE_L ("L", 40), | |
KEYCODE_M ("M", 41), | |
KEYCODE_N ("N", 42), | |
KEYCODE_O ("O", 43), | |
KEYCODE_P ("P", 44), | |
KEYCODE_Q ("Q", 45), | |
KEYCODE_R ("R", 46), | |
KEYCODE_S ("S", 47), | |
KEYCODE_T ("T", 48), | |
KEYCODE_U ("U", 49), | |
KEYCODE_V ("V", 50), | |
KEYCODE_W ("W", 51), | |
KEYCODE_X ("X", 52), | |
KEYCODE_Y ("Y", 53), | |
KEYCODE_Z ("Z", 54), | |
KEYCODE_COMMA ("COMMA", 55), | |
KEYCODE_PERIOD ("PERIOD", 56), | |
KEYCODE_ALT_LEFT ("ALT_LEFT", 57), | |
KEYCODE_ALT_RIGHT ("ALT_RIGHT", 58), | |
KEYCODE_SHIFT_LEFT ("SHIFT_LEFT", 59), | |
KEYCODE_SHIFT_RIGHT ("SHIFT_RIGHT", 60), | |
KEYCODE_TAB ("TAB", 61), | |
KEYCODE_SPACE ("SPACE", 62), | |
KEYCODE_SYM ("SYM", 63), | |
KEYCODE_EXPLORER ("EXPLORER", 64), | |
KEYCODE_ENVELOPE ("ENVELOPE", 65), | |
KEYCODE_ENTER ("ENTER", 66), | |
KEYCODE_DEL ("DEL", 67), | |
KEYCODE_GRAVE ("GRAVE", 68), | |
KEYCODE_MINUS ("MINUS", 69), | |
KEYCODE_EQUALS ("EQUALS", 70), | |
KEYCODE_LEFT_BRACKET ("LEFT_BRACKET", 71), | |
KEYCODE_RIGHT_BRACKET ("RIGHT_BRACKET", 72), | |
KEYCODE_BACKSLASH ("BACKSLASH", 73), | |
KEYCODE_SEMICOLON ("SEMICOLON", 74), | |
KEYCODE_APOSTROPHE ("APOSTROPHE", 75), | |
KEYCODE_SLASH ("SLASH", 76), | |
KEYCODE_AT ("AT", 77), | |
KEYCODE_NUM ("NUM", 78), | |
KEYCODE_HEADSETHOOK ("HEADSETHOOK", 79), | |
KEYCODE_FOCUS ("FOCUS", 80), | |
KEYCODE_PLUS ("PLUS", 81), | |
KEYCODE_MENU ("MENU", 82), | |
KEYCODE_NOTIFICATION ("NOTIFICATION", 83), | |
KEYCODE_SEARCH ("SEARCH", 84), | |
KEYCODE_MEDIA_PLAY_PAUSE ("MEDIA_PLAY_PAUSE", 85), | |
KEYCODE_MEDIA_STOP ("MEDIA_STOP", 86), | |
KEYCODE_MEDIA_NEXT ("MEDIA_NEXT", 87), | |
KEYCODE_MEDIA_PREVIOUS ("MEDIA_PREVIOUS", 88), | |
KEYCODE_MEDIA_REWIND ("MEDIA_REWIND", 89), | |
KEYCODE_MEDIA_FAST_FORWARD ("MEDIA_FAST_FORWARD", 90), | |
KEYCODE_MUTE ("MUTE", 91); | |
private String name; | |
private int keyCode; | |
private HKKeyCodeMap(String name, int keyCode) | |
{ | |
this.name = name; | |
this.keyCode = keyCode; | |
} | |
public String getName() | |
{ | |
return name; | |
} | |
public int getKeyCode() | |
{ | |
return keyCode; | |
} | |
public static HKKeyCodeMap valueOf(int keyCode) | |
{ | |
for (HKKeyCodeMap keyCodeMap : values()) { | |
if (keyCodeMap.getKeyCode() == keyCode) { | |
return keyCodeMap; | |
} | |
} | |
return HKKeyCodeMap.KEYCODE_UNKNOWN; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mituliii.hardwarekeysupportsample; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.view.KeyEvent; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
public class HKMain extends Activity | |
{ | |
protected Context context; | |
protected TextView textView; | |
@Override | |
public void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
final Context context = this; | |
textView = (TextView) findViewById(R.id.textview); | |
// 各ボタンにイベントリスナーを登録 | |
Button button1 = (Button) findViewById(R.id.button1); | |
button1.setOnClickListener(new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View view) | |
{ | |
onClickButton1(); | |
} | |
}); | |
Button button2 = (Button) findViewById(R.id.button2); | |
button2.setOnClickListener(new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View view) | |
{ | |
onClickButton2(); | |
} | |
}); | |
Button button3 = (Button) findViewById(R.id.button3); | |
button3.setOnClickListener(new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View view) | |
{ | |
onClickButton3(); | |
} | |
}); | |
// 設定画面に遷移させる | |
Button gotopreference = (Button) findViewById(R.id.gotopreference); | |
gotopreference.setOnClickListener(new View.OnClickListener() | |
{ | |
@Override | |
public void onClick(View v) | |
{ | |
Intent intent = new Intent(context, HKPreference.class); | |
startActivity(intent); | |
} | |
}); | |
} | |
// キーアップ/ダウンイベントにフックする | |
@Override | |
public boolean dispatchKeyEvent(KeyEvent event) | |
{ | |
int keyCode = event.getKeyCode(); | |
// 設定されたキーコードを取得 | |
int button1KeyCode = HKPreference.getKeyCode(this, getString(R.string.button1)); | |
int button2KeyCode = HKPreference.getKeyCode(this, getString(R.string.button2)); | |
int button3KeyCode = HKPreference.getKeyCode(this, getString(R.string.button3)); | |
if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
// キーダウンイベントにフックして | |
// 設定されたキーコードであればアクションを呼び出す | |
if (keyCode == button1KeyCode) { | |
onClickButton1(); | |
return true; | |
} else if (keyCode == button2KeyCode) { | |
onClickButton2(); | |
return true; | |
} else if (keyCode == button3KeyCode) { | |
onClickButton3(); | |
return true; | |
} | |
} else { | |
// キーアップイベントをフックして無効化する | |
if (keyCode == button1KeyCode) { | |
return true; | |
} else if (keyCode == button2KeyCode) { | |
return true; | |
} else if (keyCode == button3KeyCode) { | |
return true; | |
} | |
} | |
return super.dispatchKeyEvent(event); | |
} | |
// 各ボタンが押された時のアクション | |
protected void onClickButton1() | |
{ | |
textView.setText("button1 clicked."); | |
} | |
protected void onClickButton2() | |
{ | |
textView.setText("button2 clicked."); | |
} | |
protected void onClickButton3() | |
{ | |
textView.setText("button3 clicked."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.mituliii.hardwarekeysupportsample; | |
import android.app.AlertDialog; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.SharedPreferences; | |
import android.content.DialogInterface.OnClickListener; | |
import android.content.SharedPreferences.Editor; | |
import android.os.Bundle; | |
import android.preference.Preference; | |
import android.preference.PreferenceActivity; | |
import android.preference.PreferenceManager; | |
import android.preference.PreferenceScreen; | |
import android.view.KeyEvent; | |
import android.widget.TextView; | |
public class HKPreference extends PreferenceActivity | |
{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) | |
{ | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.layout.preference); | |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); | |
int[] ids = { | |
R.string.button1, | |
R.string.button2, | |
R.string.button3 | |
}; | |
// 各項目に設定されたキーコード取得して | |
// 動的にサマリーをセットする | |
for (int id : ids) { | |
String key = getString(id); | |
Preference preference = findPreference(key); | |
int keyCode = preferences.getInt(key, KeyEvent.KEYCODE_UNKNOWN); | |
HKKeyCodeMap keyCodeMap = HKKeyCodeMap.valueOf(keyCode); | |
String keyCodeName = keyCodeMap.getName(); | |
preference.setSummary(keyCodeName); | |
} | |
} | |
// 項目のクリックイベントにフックする | |
@Override | |
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, final Preference preference) | |
{ | |
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); | |
final Editor editor = preferences.edit(); | |
final String key = preference.getKey(); | |
// 現在設定されているキーコードを保存 | |
final int keyCode = preferences.getInt(key, KeyEvent.KEYCODE_UNKNOWN); | |
HKKeyCodeMap keyCodeMap = HKKeyCodeMap.valueOf(keyCode); | |
String keyCodeName = keyCodeMap.getName(); | |
// ダイアログにテキストビューを追加して | |
// 設定されているキーコードの名前を表示 | |
final TextView textView = new TextView(this); | |
textView.setText(keyCodeName); | |
textView.setTextSize(30); | |
// ダイアログのキーアップ/ダウンイベントにフックして | |
// 設定値とテキストビューの表示を書き換える | |
AlertDialog dialog = new AlertDialog(this) | |
{ | |
public boolean dispatchKeyEvent(KeyEvent event) | |
{ | |
int keyCode = event.getKeyCode(); | |
HKKeyCodeMap keyCodeMap = HKKeyCodeMap.valueOf(keyCode); | |
String keyCodeName = keyCodeMap.getName(); | |
editor.putInt(key, keyCode); | |
textView.setText(keyCodeName); | |
return true; | |
} | |
}; | |
dialog.setTitle(preference.getTitle()); | |
dialog.setView(textView); | |
// OKが押された時は | |
// 書き換えた設定値を適用し | |
// サマリーも新しい設定値に書き換える | |
dialog.setButton("OK", new OnClickListener() | |
{ | |
public void onClick(DialogInterface dialog, int whichButton) | |
{ | |
editor.commit(); | |
int keyCode = preferences.getInt(key, KeyEvent.KEYCODE_UNKNOWN); | |
HKKeyCodeMap keyCodeMap = HKKeyCodeMap.valueOf(keyCode); | |
String keyCodeName = keyCodeMap.getName(); | |
preference.setSummary(keyCodeName); | |
} | |
}); | |
// キャンセルが押された時は | |
// 元々設定してあった値に戻す | |
dialog.setButton2("Cancel", new OnClickListener() | |
{ | |
public void onClick(DialogInterface dialog, int whichButton) | |
{ | |
editor.putInt(key, keyCode); | |
editor.commit(); | |
} | |
}); | |
// クリアが押された時は | |
// KEYCODE_UNKNOWNを設定して閉じる | |
dialog.setButton3("Clear", new OnClickListener() | |
{ | |
public void onClick(DialogInterface dialog, int whichButton) | |
{ | |
editor.putInt(key, KeyEvent.KEYCODE_UNKNOWN); | |
editor.commit(); | |
preference.setSummary(HKKeyCodeMap.KEYCODE_UNKNOWN.getName()); | |
} | |
}); | |
dialog.show(); | |
return super.onPreferenceTreeClick(preferenceScreen, preference); | |
} | |
// キーを引数に | |
// 設定されているキーコードを取得する | |
public static int getKeyCode(Context context, String key) | |
{ | |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); | |
return preferences.getInt(key, KeyEvent.KEYCODE_UNKNOWN); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<TextView | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textSize="30sp" | |
android:layout_marginBottom="30sp" | |
android:text="@string/hello" /> | |
<TextView | |
android:id="@+id/textview" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textSize="30sp" | |
android:layout_marginBottom="30sp" | |
android:text="@string/textview" /> | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/button1" /> | |
<Button | |
android:id="@+id/button2" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/button2" /> | |
<Button | |
android:id="@+id/button3" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="30sp" | |
android:text="@string/button3" /> | |
<Button | |
android:id="@+id/gotopreference" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/gotopreference" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<PreferenceScreen | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<PreferenceScreen | |
android:key="@string/button1" | |
android:title="@string/button1" /> | |
<PreferenceScreen | |
android:key="@string/button2" | |
android:title="@string/button2" /> | |
<PreferenceScreen | |
android:key="@string/button3" | |
android:title="@string/button3" /> | |
</PreferenceScreen> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="hello">Hello World, HKMain!</string> | |
<string name="app_name">HardwareKeySupportSample</string> | |
<string name="button1">button1</string> | |
<string name="button2">button2</string> | |
<string name="button3">button3</string> | |
<string name="textview">textview</string> | |
<string name="gotopreference">go to preference</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment