Andrdoirdの向き変更通知を受け取る
地味なネタながら、検索しても直ぐには出てこなかったので
メモ替わりに・・
mainプログラムのどこかに
mContext.registerReceiver(mBroadcastReceiver,
new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED), null, null);
(※"mContext"は普通"this"の場合が多いと思います、
上記はLive壁紙で実験していたのでcontextになっています)
を入れ、その外に
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_CONFIGURATION_CHANGED.equals(action)) {
if(context.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE){
Log.d("orientation", "landscape!");
}else{
Log.d("orientation", "portrait!");
}
}
}
};
を書くのみで通知が受け取れました。
※自分の環境では、manifestファイルに
<intent-filter>
<action android:name="android.intent.action.CONFIGURATION_CHANGED" />
</intent-filter>
と書き込むことや、プログラム中で
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
を入れる必要はありませんでした。
※機種によってのような気が凄くしますが
(すごくテスティングしたわけではないが、
HT03-A実機では1回も確認できなかったので)、
横にしたとき、縦→横
と2度通知が来るときがありました。
フラグ立てたりして、あまり綺麗ではない対処が必要ですね・・。