2011年10月4日火曜日

他のアプリのリソースへのアクセス

Androidアプリを作っていて、Android OS内にあるアイコンを使いたい場合がある。
一部のアイコンは、

Drawable icon = getResources().getDrawable(android.R.drawable.ic_menu_preferences);

のように指定してリソースにアクセスできるが、できないものがある。たとえば、設定-端末-電池使用量で表示される電話/Wi-Fi/セルスタンバイ/アイドル状態のアイコンなど。これらは以下のようにすることでアクセスできる。

  Context foreignContext = context.createPackageContext("com.android.settings", Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
  Resources res = foreignContext.getResources();
  Drawable iconPowerPhone = res.getDrawable(res.getIdentifier("ic_settings_voice_calls", "drawable", "com.android.settings"));
  Drawable iconPowerRadio = res.getDrawable(res.getIdentifier("ic_settings_cell_standby", "drawable", "com.android.settings"));
  Drawable iconPowerWifi = res.getDrawable(res.getIdentifier("ic_settings_wifi", "drawable", "com.android.settings"));
  Drawable iconPowerIdle = res.getDrawable(res.getIdentifier("ic_settings_phone_idle", "drawable", "com.android.settings"));
  Drawable iconPowerBluetooth = res.getDrawable(res.getIdentifier("ic_settings_bluetooth", "drawable", "com.android.settings"));

0 件のコメント:

コメントを投稿